因为我要使用 Entity Framework Core,所以我正在尝试转换 SqlRaw 语句
var sql = @$"
update ItemList
set flag = 1
where
flag = 0 and
{groupingField} in (select {groupingField} from ItemList
where ID in (select itemID from selectedItems))
";
int noOfRowsAffected = DbContext.ExecuteSqlRaw(sql);
成一系列 LINQ 语句。
var ids = DbContext.selectedItems
.Select(x => x.itemID).ToList();
var groupIds = DbContext.ItemList
.Where(p => ids.Contains(p.Id) && p.flag == 0
.Select(p => p.GetType().GetProperty(groupingField).GetValue(p)).ToList();
var rows = DbContext.ItemList
.Where(p => groupIds.Contains(p.GetType().GetProperty(groupingField).GetValue(p)))
.ToList();
foreach(var row in rows)
{
row.flag = 1;
}
当我执行这些语句时,我在第三条语句(var rows = ...)上捕获了一个异常:
The LINQ expression 'DbSet<ItemList>
.Where(t => __groupIds_0.Contains(t.GetType().GetProperty(__groupingField_1).GetValue(t)))' could not be
translated. Either rewrite the query in a form that can be translated, or switch to client evaluation
explicitly by inserting a call to either AsEnumerable(), AsAsyncEnumerable(), ToList(), or ToListAsync().
See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
如何重写可以翻译的声明?