获得lambda Expression的深层属性

时间:2012-03-09 13:46:32

标签: linq reflection lambda

我有一些代码:

CaseHeaderComparer CaseComparer = new CaseHeaderComparer();
List<CaseHeader> CasesToProcess = new List<CaseHeader>();

foreach (GroupField fld in Fields)
{
    //get the field property - ie. Division
    System.Reflection.PropertyInfo piField = typeof(CaseHeader).GetProperty(fld.GroupFieldType.PropertyName);
    //get the item property - ie. DivisionID
    System.Reflection.PropertyInfo piItem = piField.PropertyType.GetProperty(fld.GroupFieldType.ValueMember);

    foreach (CaseHeader ch in ToProcess)
    {
        object chItem = piField.GetValue(ch, null);
        Guid ItemID = chItem != null ? (Guid)piItem.GetValue(chItem, null) : Guid.Empty;

        if (fld.Items.Select(i => i.ItemID).Contains(ItemID))
        {
            CasesToProcess.Add(ch);
        }
    }
    ToProcess = ToProcess.Except(CasesToProcess, CaseComparer).ToList();
}

我想转换为使用linq和lambdas - 我昨天在这里得到了一些帮助,相当接近:

List<CaseHeader> ToProcess = ....;
CaseHeaderComparer CaseComparer = new CaseHeaderComparer();
IEnumerable<CaseHeader> CasesToProcess = new BackingSheetCaseHeader[] { };
foreach (GroupField fld in Fields)
{
    //get the field property - ie. Division
    System.Reflection.PropertyInfo piField = typeof(CaseHeader).GetProperty(fld.GroupFieldType.PropertyName);
    //get the item property - ie. DivisionID
    System.Reflection.PropertyInfo piItem = piField.PropertyType.GetProperty(fld.GroupFieldType.ValueMember);

    CasesToProcess.Union(
        ToProcess
            .Where(c => fld.Items.Select(i => i.ItemID)
                .Contains((piField.GetValue(c, null) != null ? (Guid)piItem.GetValue(piField.GetValue(c, null), null) : Guid.Empty)))
                , CaseComparer);
}

这有效但有人指出我可以按照这个方式做点什么......

var hdr = typeof(CaseHeader);
var param = Expression.Parameter(hdr);
var cond = Expression.Condition(
    Expression.NotEqual(param, Expression.Constant(null, hdr))
,   Expression.Property(param, fld.GroupFieldType.PropertyName) <<-- but this needs to go 2 deep.. as above the item property..
,   Expression.Constant(Guid.Empty)
);
var lambda = (Func<MyCaseObj,Guid>)Expression.Lambda(cond, param).Compile();

然后我可以做

var CasesToProcess = (from csh in CasesInGroup 
    where lambda(csh).In(fld.Items.Select(i => i.ItemID)) 
    select csh);

但是2位深陷困扰我 如上所述,我需要访问CaseHeader的fld.GroupFieldType.PropertyName属性的fld.GroupFieldType.ValueMember属性。 第一级的值可能为null ..

任何人都可以给我一些指示,或者在某个地方阅读这个

感谢

1 个答案:

答案 0 :(得分:1)

您可能根本不需要手动构建Expression 。 C#编译器可以将lambda表达式转换为Func<> s Expression<Func<>>。以下工作都是:

        var func = (Func<int, bool>)(i => i == 2);
        var expression = (Expression<Func<int, bool>>)(i => i == 2);

之后expression.Compile()Func<int,bool>,其行为与func相同!所以试试吧

var expression = (Expression<Func<MyCaseObj,Guid>>)
    (c => fld.Items.Select(i => i.ItemID)
          .Contains((piField.GetValue(c, null) != null 
                     ? (Guid)piItem.GetValue(piField.GetValue(c, null), null) 
                     : Guid.Empty))
    );

从那里拿走。