lundi 12 juin 2017

How can I combine (n) expressions using LINQ?

I have a grid with (n) dynamically generated columns. One of the requirements is that a row only be shown in the grid if a non-null/non-zero value exists in one of the shown columns. I am using an Infragistics XamGrid, which has the option to use a row filter that will accept an expression as its filter criteria. I have bound the Key of my columns to MyObject properties.

I am trying to generate a chain of expressions based on which columns in my grid are shown. Here is some pseudo-code that shows an idea what I'm trying to accomplish

System.Linq.Expressions.Expression<Func<MyObject, bool>> expr = product =>
{
    foreach (var p in MyGrid.Columns.Where(co => co.DataType == typeof (decimal?) && co.Visibility == Visibility.Visible))
    {
        Expression<Func<MyObject, bool>> exprInner = lrnc => ((decimal?) lrnc.GetPropValue(p.Key)) != 0.0m;
        combined = System.Linq.Expressions.Expression.OrElse(combined.Body, exprInner.Body);
    }
};





Aucun commentaire:

Enregistrer un commentaire