相关疑难解决方法(0)

LINQ to Entities仅支持使用IEntity接口转换EDM原语或枚举类型

我有以下通用扩展方法:

public static T GetById<T>(this IQueryable<T> collection, Guid id) 
    where T : IEntity
{
    Expression<Func<T, bool>> predicate = e => e.Id == id;

    T entity;

    // Allow reporting more descriptive error messages.
    try
    {
        entity = collection.SingleOrDefault(predicate);
    }
    catch (Exception ex)
    {
        throw new InvalidOperationException(string.Format(
            "There was an error retrieving an {0} with id {1}. {2}",
            typeof(T).Name, id, ex.Message), ex);
    }

    if (entity == null)
    {
        throw new KeyNotFoundException(string.Format(
            "{0} with id {1} was not found.",
            typeof(T).Name, id));
    }

    return …
Run Code Online (Sandbox Code Playgroud)

.net c# entity-framework expression-trees dbcontext

89
推荐指数
3
解决办法
3万
查看次数

Entity Framework Core - 在 Queryable 中使用扩展方法

我有以下查询:

db.Users.AsQueryable()
    .Where(u => u.Id = userResolver.LoggedUserId() && u.Packages.Where(p => 
            p.StatusId == (int)PackageStatus.InProgress ||
            p.StatusId == (int)PackageStatus.Delivered ||
            p.StatusId == (int)PackageStatus.Shipped ||
            p.StatusId == (int)PackageStatus.Waiting) 
        .Sum(p => p.Price) > u.MaxCredit)
    .ToList()
Run Code Online (Sandbox Code Playgroud)

我想要实现的目标是将所有包状态检查分组到扩展方法中。像这样的东西:

db.Users.AsQueryable()
        .Where(u => u.Id = userResolver.LoggedUserId() &&
             u.Packages.Where(p => p.IsShippedOrInProgress())
            .Sum(p => p.Price) > u.MaxCredit)
        .ToList()


 //This is the extension method
 public static bool IsShippedOrInProgress(this Package p) {
    return p.StatusId == (int)PackageStatus.InProgress ||
           p.StatusId == (int)PackageStatus.Delivered ||
           p.StatusId == (int)PackageStatus.Shipped ||
           p.StatusId == (int)PackageStatus.Waiting)
 }
Run Code Online (Sandbox Code Playgroud)

当我查看第一个示例中生成的 sql 查询时,一切似乎都正常,但是当我使用第二种方法时,检查状态的查询部分不存在。

c# entity-framework entity-framework-core

8
推荐指数
1
解决办法
7966
查看次数

List <object> .Contains Expression Tree

我想建立一个等于预期的表达式......

Expression<Func<ReferencedEntity, bool>> expected = (ReferencedEntity referencedEntity) => foreignKeys.Contains(referencedEntity.Id);
Expression<Func<ReferencedEntity, bool>> actual;
Run Code Online (Sandbox Code Playgroud)

foreignKeys类型是一个 List<object>

这是我到目前为止,我认为它将使用Expression.Call()方法,但不确定如何去做.

ParameterExpression entityParameter = Expression.Parameter(typeof(TReferencedEntity), "referencedEntity");
MemberExpression memberExpression = Expression.Property(entityParameter, "Id");
Expression convertExpression = Expression.Convert(memberExpression, typeof(object)); //This is becuase the memberExpression for Id returns a int.

//Expression containsExpression = Expression.Call(????

//actual = Expression.Lambda<Func<TReferencedEntity, bool>>(????, entityParameter);
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

c# linq lambda

4
推荐指数
2
解决办法
5057
查看次数