nat
nat

Reputation: 2197

getting at a deep property for a lambda Expression

I have some code thus:

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();
}

Which I would like to convert to to use linq and lambdas - i got fairly close with some help from here yesterday with this:

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);
}

this works but someone pointed out that i could do something along the lines of this..

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();

then i could do

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

but the 2 deep bit is stumping me as above I need to get to the fld.GroupFieldType.ValueMember property of the fld.GroupFieldType.PropertyName property of the CaseHeader.. the value of the first level may be null..

can anyone give me some pointers, or somewhere to have a read up on this

thanks

Upvotes: 0

Views: 455

Answers (1)

AakashM
AakashM

Reputation: 63338

It may be that you don't need to build your Expression manually at all. The C# compiler can translate lambda expressions into Func<>s and Expression<Func<>>s. Both the following work:

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

after which expression.Compile() is a Func<int,bool> with the same behaviour as func! So try just

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))
    );

and take it from there.

Upvotes: 1

Related Questions