Search code examples
c#linqdynamicsystem.linq.dynamic

Linq.Dynamic FirstOrDefault() nested in OrderBy


I have the following Linq statement, which works totally fine:

query = query.OrderBy(m => m.MATERIAL_TXT.Where(mt => mt.LANG == "EN").FirstOrDefault().LTEXT);

Now I'm trying to make it dynamic by using the string based syntax from Linq.Dynamic:

query = query.OrderBy("MATERIAL_TXT.Where(LANG==\"EN\").FirstOrDefault().LTEXT");

But it throws the exception :

"No applicable aggregate method 'FirstOrDefault' exists"

It has to bedynamic so that it accepts other names instead of "MATERIAL_TXT".

What am I missing?


Solution

  • According to the documentation:

    A subset of the Standard Query Operators is supported for objects that implement IEnumerable. Specifically, the following constructs are permitted, where seq is an IEnumerable instance, predicate is a boolean expression, and selector is an expression of any type:

    • seq.Where(predicate)
    • seq.Any()
    • seq.Any(predicate)
    • seq.All(predicate)
    • seq.Count()
    • seq.Count(predicate)
    • seq.Min(selector)
    • seq.Max(selector)
    • seq.Sum(selector)
    • seq.Average(selector)

    FirstOrDefault isn't on the list, so it's reasonably safe to assume it isn't supported.