简体   繁体   English

EF 6.复杂查询。 搜索和过滤

[英]EF 6. Complex Query. Search and Filter

I'm using Entity Framework 6 in my asp.net mvc application. 我在asp.net mvc应用程序中使用Entity Framework 6。 I have complex query to database that causes about 15 tables. 我对数据库的复杂查询导致大约15个表。 Query includes searching and filtering. 查询包括搜索和过滤。 This query execution is slow (about 800 ms on local machine). 该查询执行速度很慢(在本地计算机上约为800毫秒)。

query.Include(i => i.Customer) 
     .Include(i => i.Address) 
      ... 
     .Include(i => i.Photos)
     .Select(x => new {
      ...
      x.Address.City,
      CustomerName = i.Customer != null ? i.Customer.Name : "",
      ...
    });

...
//searching & filtering
// searchFilter.PropertyName and searchFilter.PropertyValue - strings!
// for example searchFilter.PropertyName = 'CustomerName'
 query = query.Where(String.Format("{0} == {1}", searchFilter.PropertyName, searchFilter.PropertyValue));
// PageIndex = 20
 query = query.Skip(PageIndex * PageSize).Take(PageSize)
...
 var result = query.ToList();
...

The problems: 问题:

  1. Eager loading not working properly - MiniProfiler shows duplicate requests of Address table 急切加载无法正常工作-MiniProfiler显示地址表的重复请求
  2. Such searching (using 'Contains') is very limited, because I have to create anonymous type object with properties like Customer for checking if Customer is not null (or more complex actions) and have to hard-code somewhere PropertyName's strings (for instance in javascript file that calls ajax request). 这样的搜索(使用“包含”)非常有限,因为我必须创建具有客户属性的匿名类型对象,以检查客户是否不为空(或更复杂的操作),并且必须在PropertyName的字符串中进行硬编码(例如调用ajax请求的javascript文件)。

Are there other ways to do it? 还有其他方法吗?

some remarks: 一些说明:

  • you don't need to include(i => i.Address) to select x.Address.City , nor to test any Address property in a where clause 您不需要include(i => i.Address)来选择x.Address.City ,也不需要在where子句中测试任何Address属性
  • as long as you have an IQueryable bind to an Sql Server, CustomerName = i.Customer != null ? i.Customer.Name : "" 只要您具有与SQL Server的IQueryable绑定, CustomerName = i.Customer != null ? i.Customer.Name : "" CustomerName = i.Customer != null ? i.Customer.Name : "" can be replaced by CustomerName = i.Customer.Name ?? "" CustomerName = i.Customer != null ? i.Customer.Name : ""可以替换为CustomerName = i.Customer.Name ?? "" CustomerName = i.Customer.Name ?? ""

Not sure this can have significant performance impact but... 不确定这是否会对性能产生重大影响,但是...

Otherwise, as often, index creation is a path to performance improvement. 否则,索引创建通常是提高性能的途径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM