C# Linq: Combine multiple .Where() with an *OR* clause

后端 未结 4 2136
不思量自难忘°
不思量自难忘° 2021-02-19 00:08

I have been searching a lot about my current problem but I could not find a real answer to solve that issue.

I am trying to build a LINQ Query that produces the followi

4条回答
  •  长发绾君心
    2021-02-19 00:46

    Ok you have had your own share of answer about linq.

    Let me introduce a different approach using Dynamic.linq

    // You could build a Where string that can be converted to linq.
    // and do if sats and append your where sats string. as the example below
    var query = "c => (c.Field1 == \" a \" && c.Field2 == Y) || (c.Field3 == \" b \")";
    var indicator = query.Split('.').First(); // the indicator eg c
       // assume TABLE is the name of the class
    var p = Expression.Parameter(typeof(TABLE), indicator);
    var e = DynamicExpression.ParseLambda(new[] { p }, null, query);
    
    // and simple execute the expression 
    var items = Object.Where(e);
    

提交回复
热议问题