0

因为我要使用 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.

如何重写可以翻译的声明?

4

1 回答 1

0

以下扩展有助于做你想做的事,甚至支持IEnumerableIQueryable项目:

var ids = DbContext.selectedItems.Select(x => x.itemID);

var rows = DbContext.ItemList
      .FilterByItems(ids, groupingField)
      .Where(p => p.flag == 0)
      .ToList();

foreach (var row in rows)
{
      row.flag = 1;
}      

和实施:

public static class FilterExtensions
{
      public static IQueryable<TEntity> FilterByItems<TEntity, TKey>(this IQueryable<TEntity> query,
            IEnumerable<TKey> items, Expression<Func<TEntity, TKey>> entityKey)
      {
            var entityParam = entityKey.Parameters[0];

            if (items is IQueryable queryableItems)
            {
                  var containsLambda = Expression.Lambda<Func<TEntity, bool>>(
                        Expression.Call(typeof(Queryable), nameof(Queryable.Contains),
                              new[] {typeof(TKey)},
                              queryableItems.Expression,
                              entityKey.Body
                        ),
                        entityParam);
                  return query.Where(containsLambda);
            }
            else
            {
                  var containsLambda = Expression.Lambda<Func<TEntity, bool>>(
                        Expression.Call(typeof(Enumerable), nameof(Enumerable.Contains),
                              new[] {typeof(TKey)},
                              Expression.Constant(items),
                              entityKey.Body
                        ),
                        entityParam);
                  return query.Where(containsLambda);
            }
      }

      public static IQueryable<TEntity> FilterByItems<TEntity, TKey>(this IQueryable<TEntity> query,
            IEnumerable<TKey> items, string entityKey)
      {
            var entityParam     = Expression.Parameter(typeof(TEntity), "e");
            var entityKeyLambda =
                  Expression.Lambda<Func<TEntity, TKey>>(Expression.PropertyOrField(entityParam, entityKey), entityParam);

            return query.FilterByItems(items, entityKeyLambda);
      }
}
于 2021-06-03T11:26:46.510 回答