Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ查询-具有基于int数组的多个子查询的多个where_C#_Linq - Fatal编程技术网

C# LINQ查询-具有基于int数组的多个子查询的多个where

C# LINQ查询-具有基于int数组的多个子查询的多个where,c#,linq,C#,Linq,我有一个sql查询,它执行我所追求的选择类型: select * from Products p where p.ProductId in ( select distinct ProductId from ProductFacets where ProductId in (select ProductId from ProductFacets where FacetTypeId = 1) and ProductId in (select ProductId from P

我有一个sql查询,它执行我所追求的选择类型:

select * from Products p where p.ProductId in (
    select distinct ProductId from ProductFacets 
    where ProductId in (select ProductId from ProductFacets where FacetTypeId = 1)
    and ProductId in (select ProductId from ProductFacets where FacetTypeId = 4)
)
可以将多个FacetTypeID传递到此查询中

此查询在基于int[]类型的参数参数的方法中构造

public IEnumerable<Product> GetProductsByFacetTypes(string productTypeSysName, int[] facetTypeIds)
这将返回正确的结果集

但是,我不确定如何使用int[]facetTypeIds参数构建此查询

编辑:

ProductFacets包含以下数据:

ProductId, FacetTypeId
1, 1
1, 2
2, 1
2, 3
3, 4
3, 5
4, 1
4, 2
例如,我希望能够只选择FacetTypeId为1和2的产品。
结果集应该包含ProductID 1和ProductID 4编辑:对不起,我误读了问题。如果需要显示所有类型,我建议您使用并动态构建Where子句。这将直接使用扩展方法

var facetTypeIds = new [] { 1, 4, ... };
var predicate = PredicateBuilder.True<Product>();
foreach (int id in facetTypeIds)
{
    int facetId = id; // avoid capturing loop variable
    predicate = predicate.And( p => p.FacetTypes.Any( x => x.FacetTypeId == facetId );
}

var products = sc.Products
                 .Where( p => p.ProductType.SysName == productTypeSysName )
                 .Where( predicate );

编辑:对不起,我误读了这个问题。如果需要显示所有类型,我建议您使用并动态构建Where子句。这将直接使用扩展方法

var facetTypeIds = new [] { 1, 4, ... };
var predicate = PredicateBuilder.True<Product>();
foreach (int id in facetTypeIds)
{
    int facetId = id; // avoid capturing loop variable
    predicate = predicate.And( p => p.FacetTypes.Any( x => x.FacetTypeId == facetId );
}

var products = sc.Products
                 .Where( p => p.ProductType.SysName == productTypeSysName )
                 .Where( predicate );

这是基于tvanfosson的代码。不过,我对这种方法的性能表示怀疑

var facetTypeIds = new [] { 1, 4, ... };

var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
      && facetTypeIds.All(ft => p.FacetTypes.Any(x => x.FacetTypeId == ft))
select p;

这是基于tvanfosson的代码。不过,我对这种方法的性能表示怀疑

var facetTypeIds = new [] { 1, 4, ... };

var products = from p in sc.Products
where p.ProductType.SysName == productTypeSysName
      && facetTypeIds.All(ft => p.FacetTypes.Any(x => x.FacetTypeId == ft))
select p;

可以通过调用Contains将本地集合传输到数据库:

from ft in facetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft;
本地集合被转换为sql参数。Sql Server有大约2100个参数的限制,所以要小心


具有任何方面的产品

from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where 
(
  from ft in p.FacetTypes
  where facetTypeIds.Contains(ft.FacetTypeId)
  select ft
).Any()
select p;

具有所有方面的产品

int facetCount = facetTypeIds.Count();

from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where 
(
  from ft in p.FacetTypes
  where facetTypeIds.Contains(ft.FacetTypeId)
  select ft.FacetTypeId
).Distinct().Count() == facetCount
select p;

可以通过调用Contains将本地集合传输到数据库:

from ft in facetTypes
where facetTypeIds.Contains(ft.FacetTypeId)
select ft;
本地集合被转换为sql参数。Sql Server有大约2100个参数的限制,所以要小心


具有任何方面的产品

from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where 
(
  from ft in p.FacetTypes
  where facetTypeIds.Contains(ft.FacetTypeId)
  select ft
).Any()
select p;

具有所有方面的产品

int facetCount = facetTypeIds.Count();

from p in sc.Products
where p.ProductType.SysName == productTypeSysName
where 
(
  from ft in p.FacetTypes
  where facetTypeIds.Contains(ft.FacetTypeId)
  select ft.FacetTypeId
).Distinct().Count() == facetCount
select p;


不幸的是,这并没有产生我想要的结果。这将显示FacetTypeId为1或4的所有产品。我的Linq查询只显示FacetTypeId为1和4@sf-我误读了你的问题(实际上,只是略读了一下——对不起)。我已经根据您的实际要求更新了我的答案。编辑后的答案看起来不错。这比一个简单的调用要密集一些,但如果查询的数据集很大,这是唯一可行的选择。为更新干杯。我误读了上一次更新,只看到了谓词样式的代码。查询正在运行,但我无法循环遍历products变量。我收到一个错误:“LINQ to Entities中不支持LINQ表达式节点类型‘Invoke’。@sf-不知道您正在使用LINQ to Entities。PredicateBuilder必须依赖一些LINQ到SQL实现细节。我想它可以修改为与LINQtoEntities一起使用,但我还没有将它与它们一起使用。它本质上是在做您答案中的代码正在做的事情,但是只构建where子句,然后应用它,而不是简单地进一步过滤它。它比我展示的更强大,因为你可以创建任意复杂的条件。但是,如果它对实体不起作用,那也没什么帮助。不幸的是,这并不能产生我想要的结果。这将显示FacetTypeId为1或4的所有产品。我的Linq查询只显示FacetTypeId为1和4@sf-我误读了你的问题(实际上,只是略读了一下——对不起)。我已经根据您的实际要求更新了我的答案。编辑后的答案看起来不错。这比一个简单的调用要密集一些,但如果查询的数据集很大,这是唯一可行的选择。为更新干杯。我误读了上一次更新,只看到了谓词样式的代码。查询正在运行,但我无法循环遍历products变量。我收到一个错误:“LINQ to Entities中不支持LINQ表达式节点类型‘Invoke’。@sf-不知道您正在使用LINQ to Entities。PredicateBuilder必须依赖一些LINQ到SQL实现细节。我想它可以修改为与LINQtoEntities一起使用,但我还没有将它与它们一起使用。它本质上是在做您答案中的代码正在做的事情,但是只构建where子句,然后应用它,而不是简单地进一步过滤它。它比我展示的更强大,因为你可以创建任意复杂的条件。但是,如果它不能与实体一起工作,那就没有多大帮助。是的,任何在数据库和内存中的IEnumerables之间交叉的地方都会被SQL生成器“强制执行”。不过,这对小“n”有好处!:)嗯,事实证明这比我想象的要复杂。数据集将相当大,而且也会经常被访问。这将不起作用。您将得到一个异常“除了Contains运算符之外,不能在查询运算符的LINQ到SQL实现中使用本地序列”。这意味着,如果在包含表达式中使用facetTypeIds int数组,则只能传递该数组。此查询仍然是提取正确结果集的查询。然而,我在尝试应用.Skip时发现了一个问题。是的,任何在数据库和内存中的IEnumerables之间交叉的地方都会被SQL生成器“强制执行”。不过,这对小“n”有好处!:)嗯,事实证明这比我想象的要复杂。数据集将相当大,而且也会经常被访问。这将不起作用。您将得到一个异常“除了Contains运算符之外,不能在查询运算符的LINQ到SQL实现中使用本地序列”。这意味着,如果在包含表达式中使用facetTypeIds int数组,则只能传递该数组。此查询仍然是提取正确结果集的查询。但是,我在尝试应用时发现了一个问题。请跳到最终结果。谢谢。我刚刚测试了一下,结果出了问题。我在寻找拥有所有FacetTypeID的产品。例如:同时具有FacetTypeId和4的产品,但不具有仅具有FacetTypeId的产品1@david-b关于Sql Server限制--th