【发布时间】:2021-02-19 05:00:11
【问题描述】:
[已编辑]
我正在使用带有 EF 6 的 ASP.NET MVC。 在显示有关客户姓名和客户地址的所有信息的网页上。现在我想根据客户名称或客户地址过滤记录。 以下是我在客户表和地址表中搜索 searchText 的逻辑,但它仍然仅按客户名称搜索。
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();
【问题讨论】:
-
其实我什么都不懂……
-
在给定的图片中,搜索框正在根据 Customer Name Only 进行搜索。与它一起,searchText 还应该在客户地址或站点地址中搜索。如果找到任何一个或两个,则应显示相应的客户地址和客户名称。
标签: c# linq asp.net-mvc-4 asp.net-web-api entity-framework-6