LINQ的表达与一般的财产 [英] LINQ expression with generic property

查看:119
本文介绍了LINQ的表达与一般的财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是有关这一个: LINQ表达与 - 通用级-properties



这一次,我想获得具有共同的ID新创建的对象。该ID,其实是外键,因此可以有不同的名称。



我想出了以下内容:

 公共静态的IEnumerable< T> GETNEW< TID,T>(IQueryable的< T>对象,TID编号,日期时间日期表达式来; Func键< T,TID>> idSelector)
,其中T:类,ISyncable< TID>
{
返回objects.Where(O => idSelector(O)== ID和放大器;&安培; o.CreatedDate>日期);
}



该方法将被称为是这样的:

  VAR修改= GETNEW(dbObjects,编号,日期,实体= GT; entity.PersonId); 



不幸的是我得到的错误:

 'idSelector'是一个变量,但使用的方法等。 

传递给该方法的表达应该做这样的事情:

  objects.Where(O = GT; o.PersonId == ID和放大器;&安培; o.CreatedDate>日期); 


解决方案

您需要建立一个新的表达代表的条件,传递到其中,

 公共静态的IEnumerable< T> GETNEW< TID,T>(IQueryable的< T>对象,TID编号,日期时间日期表达式来; Func键< T,TID>> idSelector)
,其中T:类,ISyncable< TID>
{
VAR paramExpr = Expression.Parameter(typeof运算(T));
VAR idEqExpr = Expression.Equal(Expression.Invoke(idSelector,paramExpr),Expression.Constant(ID));
变种createdPropExpr = Expression.Property(paramExprCreatedDate);
VAR gtExpr = Expression.GreaterThan(createdPropExpr,Expression.Constant(日期));
VAR andExpr = Expression.And(idEqExpr,gtExpr);

VAR condExpr = Expression.Lambda<&Func键LT; T,BOOL>>(andExpr,paramExpr);

返回objects.Where(condExpr);
}



编辑:如果你知道 idSelector 是一个属性的表情,你可以提取所引用的属性,并创建一个新的属性表达式,而不是使用调用的:

  VAR idProperty =(的PropertyInfo)((MemberExpression)idSelector.Body)。成员; 
VAR idEqExpr = Expression.Equal(Expression.Property(paramExpr,idProperty),Expression.Constant(ID));


My question is related to this one: linq-expression-with-generic-class-properties

This time I would like to get newly created objects which have the id in common. The id is in fact the foreign key and therefore can have different names.

I came up with the following:

public static IEnumerable<T> GetNew<TId, T>(IQueryable<T> objects, TId id, DateTime date, Expression<Func<T, TId>> idSelector) 
    where T : class, ISyncable<TId>
{
    return objects.Where(o => idSelector(o) == id && o.CreatedDate > date);
}

The method would be called like this:

var modified = GetNew(dbObjects, id, date, entity => entity.PersonId);

Unfortunately I get the error:

'idSelector' is a variable but is used like a method.

The expression passed to the method should do something like that:

objects.Where(o => o.PersonId == id && o.CreatedDate > date);

解决方案

You need to build a new expression representing the condition and pass that to Where:

public static IEnumerable<T> GetNew<TId, T>(IQueryable<T> objects, TId id, DateTime date, Expression<Func<T, TId>> idSelector)
    where T : class, ISyncable<TId>
{
    var paramExpr = Expression.Parameter(typeof(T));
    var idEqExpr = Expression.Equal(Expression.Invoke(idSelector, paramExpr), Expression.Constant(id));
    var createdPropExpr = Expression.Property(paramExpr, "CreatedDate");
    var gtExpr = Expression.GreaterThan(createdPropExpr, Expression.Constant(date));
    var andExpr = Expression.And(idEqExpr, gtExpr);

    var condExpr = Expression.Lambda<Func<T, bool>>(andExpr, paramExpr);

    return objects.Where(condExpr);
}

EDIT: If you know that the idSelector is a property expression, you could extract the referenced property and create a new property expression instead of using Invoke:

var idProperty = (PropertyInfo)((MemberExpression)idSelector.Body).Member;
var idEqExpr = Expression.Equal(Expression.Property(paramExpr, idProperty), Expression.Constant(id));

这篇关于LINQ的表达与一般的财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆