0
我有3个表定义如下:LINQ的联合与组由perrformnces问题
public Class Product
{
public long Key {get; set;}
public CountryKey {get; set;}
}
public Class ProductCountry
{
pullic long Key {get; set;}
public long ProductKey {get; set;}
public LocalProductKey {get; set;}
public long CountryKey {get; set;}
}
public Class Country
{
public long Key {get; set;}
public string Name {get; set;}
}
我使用EF数据库首先,为每个类,我们有一个观点,从数据库(VW_Product
获取数据,以获得产品)
我有一种方法筛选产品的集合取决于一个标准。
IQueryable<VW_Product> query1 = FilterQuery(Object criteria);
获取符合条件的所有产品;
现在我希望通过添加如下因素集合Concat的的filtred集合:
var countriesKey = new List<long>() {45, 36, 6974, 366,....};
var keys = Context.VW_ProductCountries
.GroupBy(pc => pc.ProductKey)
.Where(grp => grp.Any(pc => countriesKey.Contains(pc.CountryKey) && !grp.Any(x => x.LocalProductKey != null)))
.SelectMany(grp => grp.Select(pc => pc.Productkey))
.Distinct();
var query2 = Context.VW_Product.Where(p => keys.Contains(p.ProductKey));
var result = query1.Concat(query2);
是提高此查询,因为它需要大量的时间来执行的另一种方式。
这将有助于如果你的代码是正确的或编译的,(这是什么类型的'LocalProductkey'?什么是'query1'?等等。 ),或者有意义('!grp.Any(x => x.LocalProductKey!= null)'基本上将结果过滤为'LocalProductKey == null'),无论如何,似乎查询比它更复杂需要,特别是那些往往会导致非常糟糕的SQL的分组似乎是不必要的。 –