我正在尝试将 Web API 从 .NET Core 2.2 迁移到 .NET Core 3.0,但我偶然发现了以下内容:
public Dictionary<int, Tag> GetTagMap(IList<int> tagIds = null)
{
var tags = context.Tag.AsNoTracking();
if (tagIds != null)
tags = tags.Where(t => tagIds.Contains(t.TagId));
return tags
.ToList() // explicit client evaluation in 3.0
.ToDictionary(t => t.TagId, t => t);
}
Run Code Online (Sandbox Code Playgroud)
这用于生成与此类似的 SQL 语句:
SELECT TagId, Name FROM Tag WHERE TagId IN (1, 2, 3)
Run Code Online (Sandbox Code Playgroud)
这对于正确索引的列和少量IN值非常有效。
现在我收到以下错误提示List<>.Contains不再支持翻译:
System.InvalidOperationException: '无法翻译 LINQ 表达式'Where( source: DbSet, predicate: (t) => (Unhandled parameter: __tagIds_0).Contains(t.TagId))'。以可翻译的形式重写查询,或通过插入对 …
entity-framework-core .net-core-3.0 entity-framework-core-3.0
我有以下查询接收一个int列表作为参数:
public int GetMostRecent(List<int> TheIDs)
{
...using MyDC...
var TheMostRecentID = (from d in MyDC.Data
where TheIDs.Contains(d.ID)
orderby d.DateTime
select d.ID).LastOrDefault();
}
Run Code Online (Sandbox Code Playgroud)
这是将参数集合中的列表与数据库中的数据进行匹配的最佳方法,还是比使用linq-to-sql中的.Contains()方法更好的方法.
谢谢.