Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linq 获取lambda表达式的deep属性_Linq_Reflection_Lambda - Fatal编程技术网

Linq 获取lambda表达式的deep属性

Linq 获取lambda表达式的deep属性,linq,reflection,lambda,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.Prope

因此,我有一些代码:

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();
}
但这两个深的部分让我难堪 如上所述,我需要访问CaseHeader的fld.GroupFieldType.PropertyName属性的fld.GroupFieldType.ValueMember属性。。 第一级的值可能为空

有谁能给我一些建议,或是找个地方读一读


谢谢

可能您根本不需要手动构建
表达式。C#编译器可以将lambda表达式转换为
Func
s
表达式
s。这两项工作包括:

        var func = (Func<int, bool>)(i => i == 2);
        var expression = (Expression<Func<int, bool>>)(i => i == 2);
从那里开始

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);
        var func = (Func<int, bool>)(i => i == 2);
        var expression = (Expression<Func<int, bool>>)(i => i == 2);
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))
    );