[英]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: 问题:
Are there other ways to do it? 还有其他方法吗?
some remarks: 一些说明:
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属性 CustomerName = i.Customer != null ? i.Customer.Name : ""
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.