没有多个选项的多过滤器搜索 -ASP.NET MVC
Multiple filter Searching without having multiple options -ASP.NET MVC
[已编辑]
我正在使用 ASP.NET MVC 和 EF 6。
在显示有关客户姓名和客户地址的所有信息的网页上。现在我想根据客户名称或客户地址过滤记录。
下面是我在客户 table 和地址 Table 中搜索搜索文本的逻辑,但它仍然只按客户名称搜索。
public class Address
{
public int AdressID;
public string Street;
public string city;
public string Zip;
}
public class Customer
{
public int CustomerId;
public string CustomerName;
public string AdressId;
public int CustomerActive;
public virtual Address Address;
public virtual ICollection<Site> Sites;
}
这就是我在控制器中所做的。
public AllCustomerList GetCustomersV2WithFilterAsync(FilterQuery fr)
{
var searchText = GetFilterForSearchText(fr, FilterType.SearchText);
this.DatabaseContext.DisableLazyLoading();
var totalCount = 0;
var customerQuery = this.DatabaseContext.Customers.AsNoTracking().Where(p =>
p.customerActive.HasValue && p.customerActive.Value)
.Include(p => p.Address)
.Include(p => p.ExternalSystemCustomerIntegrations)
.Include(p => p.Address1)
if (!string.IsNullOrEmpty(searchText))
{
//This query will search the string in customer table
customerQuery = customerQuery.Where(c => c.customerName.Contains(searchText));
//In this following code section the searchtext will be queried for customer address and customer Name
// searching according to customer Adress
customerQuery = customerQuery.Where(c =>
c.Address.street.Contains(searchText)
|| c.Address.street2.Contains(searchText)
|| c.Address.city.Contains(searchText)
|| c.Address.County.Contains(searchText)
|| c.Address.zip.Contains(searchText)
);
}
customerQuery = customerQuery.OrderByDescending(p => p.customerID);
totalCount = customerQuery.Count();
var customers = customerQuery.GetPaginatedData(fr.Offset, fr.RecordSet, totalCount > 0).ToList();
这里有几点需要注意。
构建 LINQ 查询时,只有在需要实现数据时才会执行。
到那时为止,查询将保留为可以执行的表达式。
您可以通过多种方式强制执行查询,例如,在查询末尾 .ToList()
。
另一件事是,在您的代码中,您搜索地址详细信息的逻辑是在您的客户名称搜索之后。但重要的是,它只是添加到客户姓名查询中。因此,从本质上讲,只有当搜索文本包含在他们的姓名和地址的一部分中时,您才能找到客户。
您可以通过多种方式解决这个问题。
或者,结合名称和地址查询:
if (!string.IsNullOrEmpty(searchText))
{
customerQuery =
customerQuery.Where(c => c.customerName.Contains(searchText)
|| c.Address.street.Contains(searchText)
|| c.Address.street2.Contains(searchText)
|| c.Address.city.Contains(searchText)
|| c.Address.County.Contains(searchText)
|| c.Address.zip.Contains(searchText));
}
或者生成两个单独的查询:
if (!string.IsNullOrEmpty(searchText))
{
var customerNameQuery =
customerQuery.Where(c => c.customerName.Contains(searchText));
var customerAddressQuery =
customerQuery.Where(c => c.Address.street.Contains(searchText)
|| c.Address.street2.Contains(searchText)
|| c.Address.city.Contains(searchText)
|| c.Address.County.Contains(searchText)
|| c.Address.zip.Contains(searchText));
// ... use the new queries ...
}
[已编辑]
我正在使用 ASP.NET MVC 和 EF 6。 在显示有关客户姓名和客户地址的所有信息的网页上。现在我想根据客户名称或客户地址过滤记录。 下面是我在客户 table 和地址 Table 中搜索搜索文本的逻辑,但它仍然只按客户名称搜索。
public class Address
{
public int AdressID;
public string Street;
public string city;
public string Zip;
}
public class Customer
{
public int CustomerId;
public string CustomerName;
public string AdressId;
public int CustomerActive;
public virtual Address Address;
public virtual ICollection<Site> Sites;
}
这就是我在控制器中所做的。
public AllCustomerList GetCustomersV2WithFilterAsync(FilterQuery fr)
{
var searchText = GetFilterForSearchText(fr, FilterType.SearchText);
this.DatabaseContext.DisableLazyLoading();
var totalCount = 0;
var customerQuery = this.DatabaseContext.Customers.AsNoTracking().Where(p =>
p.customerActive.HasValue && p.customerActive.Value)
.Include(p => p.Address)
.Include(p => p.ExternalSystemCustomerIntegrations)
.Include(p => p.Address1)
if (!string.IsNullOrEmpty(searchText))
{
//This query will search the string in customer table
customerQuery = customerQuery.Where(c => c.customerName.Contains(searchText));
//In this following code section the searchtext will be queried for customer address and customer Name
// searching according to customer Adress
customerQuery = customerQuery.Where(c =>
c.Address.street.Contains(searchText)
|| c.Address.street2.Contains(searchText)
|| c.Address.city.Contains(searchText)
|| c.Address.County.Contains(searchText)
|| c.Address.zip.Contains(searchText)
);
}
customerQuery = customerQuery.OrderByDescending(p => p.customerID);
totalCount = customerQuery.Count();
var customers = customerQuery.GetPaginatedData(fr.Offset, fr.RecordSet, totalCount > 0).ToList();
这里有几点需要注意。
构建 LINQ 查询时,只有在需要实现数据时才会执行。
到那时为止,查询将保留为可以执行的表达式。
您可以通过多种方式强制执行查询,例如,在查询末尾 .ToList()
。
另一件事是,在您的代码中,您搜索地址详细信息的逻辑是在您的客户名称搜索之后。但重要的是,它只是添加到客户姓名查询中。因此,从本质上讲,只有当搜索文本包含在他们的姓名和地址的一部分中时,您才能找到客户。
您可以通过多种方式解决这个问题。
或者,结合名称和地址查询:
if (!string.IsNullOrEmpty(searchText))
{
customerQuery =
customerQuery.Where(c => c.customerName.Contains(searchText)
|| c.Address.street.Contains(searchText)
|| c.Address.street2.Contains(searchText)
|| c.Address.city.Contains(searchText)
|| c.Address.County.Contains(searchText)
|| c.Address.zip.Contains(searchText));
}
或者生成两个单独的查询:
if (!string.IsNullOrEmpty(searchText))
{
var customerNameQuery =
customerQuery.Where(c => c.customerName.Contains(searchText));
var customerAddressQuery =
customerQuery.Where(c => c.Address.street.Contains(searchText)
|| c.Address.street2.Contains(searchText)
|| c.Address.city.Contains(searchText)
|| c.Address.County.Contains(searchText)
|| c.Address.zip.Contains(searchText));
// ... use the new queries ...
}