gpt4 book ai didi

c# - Linq to SQL问题

转载 作者:行者123 更新时间:2023-11-30 12:18:16 25 4
gpt4 key购买 nike

我有一个记录 ID(整数)的本地集合。

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

这是我的查询:

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;

我收到这个错误:

Local sequence cannot be used in Linq to SQL implementation of query operators except the Contains() operator.

我知道 Linq to SQL 不支持 .All(),但是有没有办法做我想做的事情?

最佳答案

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

from c in myDC.Customer
where c.Orders.All(o => myList.Contains(o.ID))
select c;

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

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;

客户在哪里内存集合中的 OrderId 设置为等于子集合中的 ID。

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 将拒绝参数数量。

关于c# - Linq to SQL问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2525764/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com