gpt4 book ai didi

c# - 如果不支持包含,如何在 LINQ to Entities( Entity Framework )中执行 SQL 样式 'IN' 语句?

转载 作者:可可西里 更新时间:2023-11-01 08:10:17 25 4
gpt4 key购买 nike

我正在使用 LINQ to Entities(不是 LINQ to SQL),但在创建“IN”样式查询时遇到了问题。这是我目前的查询:

var items = db.InventoryItem
.Include("Kind")
.Include("PropertyValues")
.Include("PropertyValues.KindProperty")
.Where(itm => valueIds.Contains(itm.ID)).ToList<InventoryItem>();

然而,当我这样做时,会抛出以下异常:

LINQ to Entities does not recognize the method 'Boolean Contains(Int64)' method, and this method cannot be translated into a store expression.

有人对此有解决方法或其他解决方案吗?

最佳答案

你需要使用这个:

.Where(string.Format("it.ID in {0}", string.Join(",", valueIds.ToArray())));

或者动态构造 WHERE 部分,如 this邮政。

附言- 信息已更新,此答案更新如下以保持相关性:

引用的链接包含以下更新:

...in EF4 we added support for the Contains method and at least in this specific case for collection-valued parameters. Therefore this kind of code now works right out of the box and it is not necesary to use any additinal expression building method:

var statusesToFind = new List<int> {1, 2, 3, 4};
var foos = from foo in myEntities.Foos
where statusesToFind.Contains(foo.Status)
select foo;

关于c# - 如果不支持包含,如何在 LINQ to Entities( Entity Framework )中执行 SQL 样式 'IN' 语句?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/328600/

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