Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/321.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到SQL问题_C#_.net_Asp.net_Linq_Linq To Sql - Fatal编程技术网

C# Linq到SQL问题

C# Linq到SQL问题,c#,.net,asp.net,linq,linq-to-sql,C#,.net,Asp.net,Linq,Linq To Sql,我有一个记录Id的整数的本地集合 我需要检索在本地集合中具有每个子记录ID的记录 from c in myDC.Customer where c.Orders.All(o => myList.Contains(o.ID)) select c; from c in myDC.Customers where (from o in c.Orders where myList.Contains(o.ID) group o.ID by o.ID).Distinct().Count(

我有一个记录Id的整数的本地集合

我需要检索在本地集合中具有每个子记录ID的记录

from c in myDC.Customer
where c.Orders.All(o => myList.Contains(o.ID))
select c;
from c in myDC.Customers
where (from o in c.Orders
    where myList.Contains(o.ID)
    group o.ID by o.ID).Distinct().Count() == myList.Count()
select c;
from c in myDC.Customers
let Ids = c.Orders.Select(o => o.ID).Distinct()
where Ids.Count() == myList.Count()
  && Ids.All(id => myList.Contains(id))
select c;
我的问题是:

public List<int> OwnerIds { get; private set; }
...
filteredPatches = from p in filteredPatches
                  where OwnerIds.All(o => p.PatchesOwners.Select(x => x.OwnerId).Contains(o))
                  select p;
我得到这个错误:


除Contains运算符外,查询运算符的Linq to SQL实现中不能使用局部序列


我明白。Linq-to-SQL不支持所有这些,但有什么方法可以实现我想做的吗?

你可以加入OwnerIds吗?

我不知道用Linq-to-SQL实现这一点的方法。问题是,您需要将列表传送到服务器,以便服务器可以查询它。您的列表位于计算机的内存中,SQL Server需要在服务器上进行筛选

对于直接SQL,您可以使用带有in运算符的常规SELECT语句来实现这一点。不要超过1000件物品放在抽屉里

您可以将所有ID插入SQL中的临时表中,然后将其连接到可以使用LINQ的表中,但这需要两个步骤—插入时假设您有一个集合表,然后执行连接查询,然后执行清理查询以删除集合


如果未筛选的结果集可能很大,您可以在不带筛选条件的情况下进行LINQ查询,然后在内存集中进行筛选。如果未筛选的结果集可能很大,则不建议使用此筛选。

错误表示,本地序列意味着除Contains运算符外,查询运算符的LINQ to SQL实现中不能使用OwnerID。 因此,您可以:

1从SQL加载所有filteredPatches行

var loadedData = filteredPatches.Select(i => i).ToList();
2将数据过滤为简单的局部序列

var result = loadedData.Where(i => i.PatchesOwners.All(o => OwnerIds.Contains(o.ID)));

编译器说的是

OwnerIds.Contains(someVariable)
支持,并将其翻译为:

WHERE someVariable IN (OwnerId1, OwnerId2, OwnerIdN)
现在,我们没有您查询的所有信息,但是如果您可以重新制定您尝试使用Contains所做的操作,您就可以了。

客户在哪里 子集合中的OrderID是内存集合中ID的子集

from c in myDC.Customer
where c.Orders.All(o => myList.Contains(o.ID))
select c;
from c in myDC.Customers
where (from o in c.Orders
    where myList.Contains(o.ID)
    group o.ID by o.ID).Distinct().Count() == myList.Count()
select c;
from c in myDC.Customers
let Ids = c.Orders.Select(o => o.ID).Distinct()
where Ids.Count() == myList.Count()
  && Ids.All(id => myList.Contains(id))
select c;
客户在哪里 内存中集合中的OrderID是子集合中ID的子集

from c in myDC.Customer
where c.Orders.All(o => myList.Contains(o.ID))
select c;
from c in myDC.Customers
where (from o in c.Orders
    where myList.Contains(o.ID)
    group o.ID by o.ID).Distinct().Count() == myList.Count()
select c;
from c in myDC.Customers
let Ids = c.Orders.Select(o => o.ID).Distinct()
where Ids.Count() == myList.Count()
  && Ids.All(id => myList.Contains(id))
select c;
客户在哪里 内存中集合中的OrderID设置为等于子集合中的ID

from c in myDC.Customer
where c.Orders.All(o => myList.Contains(o.ID))
select c;
from c in myDC.Customers
where (from o in c.Orders
    where myList.Contains(o.ID)
    group o.ID by o.ID).Distinct().Count() == myList.Count()
select c;
from c in myDC.Customers
let Ids = c.Orders.Select(o => o.ID).Distinct()
where Ids.Count() == myList.Count()
  && Ids.All(id => myList.Contains(id))
select c;
所有这些都为我生成了sql

PS-这些假设ID在myList中已经是不同的。如果还没有,请使用:

myList = myList.Distinct().ToList();

PSS-适用于多达2000项的列表。高于该值将被转换为sql,然后sql server将在参数数量上呕吐。

除了Contains运算符。另外-为什么不在in中检查1000个项目?我已经做了很多次,但都没有结果。@DavidB-不知道contains操作符。这就是为什么我说我不知道一种方法,而不是不知道。对于超过1000个项目,我们实际上用它进行了测试,发现在某些情况下,您的查询性能会下降。如果您有超过1000个项目,您可以通过将其分为两个或多个查询来获得更好的查询性能。如果您想验证每个列表中是否都有某些内容。。您不能使用任何不包含。。。