Dynamic LINQ: Comparing Nested Data With Parent Property

c#-4.0 dynamic-linq expression lambda linq

Question

I've a class with following structure:

 public class BestWayContext
{
    public Preference Preference { get; set; }
    public DateTime DueDate { get; set; }
    public List<ServiceRate> ServiceRate { get; set; }
}

public class ServiceRate
{
    public int Id { get; set; }
    public string Carrier { get; set; }
    public string Service { get; set; }
    public decimal Rate { get; set; }
    public DateTime DeliveryDate { get; set; }
}

and I've dynamic linq expression string

"Preference != null && ServiceRate.Any(Carrier == Preference.Carrier)"

and I want to convert above string in Dynamic LINQ as follows:

  var expression = System.Linq.Dynamic.DynamicExpression.ParseLambda<BestWayContext, bool>(condition, null).Compile();

But it showing following error:

enter image description here

Please correct me what am I doing wrong?

1
3
1/1/2015 8:54:18 AM

Popular Answer

You want to use Preference which belong to BestWayContext but you didn't tell the compiler about that. If i write your expression on Linq i will do as follows:

[List of BestWayContext].Where(f => f.Preference != null && f.ServiceRate.Where(g => g.Carrier == f.Preference.Carrier) );

As you see i specified to use Preference of BestWayContext.

0
1/1/2015 9:58:06 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow