0

I need to configure the condition in config and same thing will be used as condition for lambda expression Where clause. Below is the code i tried but all the list items are getting set with this value.

Test2 tt = new Test2();
tt.s2 = "TEST1";
tt.s3 = "TEST2";
tt.s4 = "TEST3";

Test2 tt1 = new Test2();
tt1.s2 = "TEST11";
tt1.s3 = "TEST21";
tt1.s4 = "TEST31";

Test2 tt2 = new Test2();
tt2.s2 = "TEST12";
tt2.s3 = "TEST22";
tt2.s4 = "TEST32";

List<Test2> test = new List<Test2>();
test.Add(tt);
test.Add(tt1);
test.Add(tt2);

var cond = "item => item.s2 == TEST1";
var test2List = test.Select(item => cond);

So, can anyone suggest how can I achieve this dynamic concept?

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Hanumantha
  • 107
  • 2
  • 12
  • I know you can. Its arduous. Read up on parsing Expression, Predicate, Lamda. – Mr. B Mar 29 '16 at 12:13
  • 3
    Tried Dynamic LINQ (http://dynamiclinq.azurewebsites.net/) ? – rbm Mar 29 '16 at 12:15
  • Why is your condition in a string? *How dynamic* does it need to be? – Stefan Steinegger Mar 29 '16 at 12:17
  • Is your condition defined as `string` which has to be evaluated into expression at run-time? Or what exactly you call *dynamic*? If you need some kind of filtering, then setting `filter` and doing e.g. `test.Select(item => item[1].Contains(filter))` is not really *dynamic*. – Sinatr Mar 29 '16 at 12:18
  • @Sinatr, Yes I need to evaluate string as expression at run time – Hanumantha Mar 29 '16 at 12:25
  • Search for "c# compile string at runtime" (should lead you to [`CodeDOM`](http://stackoverflow.com/a/826435/1997232) examples, where you can wrap condition into a method, compile it and call when needed) and similar (expressions, @rbm comment is a perfect match for you). – Sinatr Mar 29 '16 at 12:35
  • @Hanumantha: "Yes I need to evaluate string as expression at run time." To do what? Again: how dynamic does it need to be? compiling code at runtime is probably the most risky and most demanding solution of all (e.g. considering performance or security). I would search for other possible solutions before going that far. – Stefan Steinegger Mar 29 '16 at 13:09

4 Answers4

1

If you're trying to have your conditions expressed as strings then dynamic LINQ can do that for you. If you reference System.Linq.Dynamic then you can write conditions like

var cond = "s2 == \"TEST1\"";
var test2List = test.Where(cond);
test2List.ToList().ForEach(_ => Console.WriteLine($"{_.s2}, {_.s3}. {_.s4}"));


cond = "s2 == \"TEST1\" || s2 == \"TEST12\"";
test2List = test.Where(cond);
test2List.ToList().ForEach(_ => Console.WriteLine($"{_.s2}, {_.s3}. {_.s4}"));

which produces

TEST1, TEST2. TEST3
TEST1, TEST2. TEST3
TEST12, TEST22. TEST32
rbm
  • 3,243
  • 2
  • 17
  • 28
  • Is "System.Linq.Dynamic" is Microsoft owned library? – Hanumantha Mar 29 '16 at 12:54
  • The original code was written by Microsoft, the port of the code (update for .NET 4) is maintained by non-MS guys, see http://dynamiclinq.azurewebsites.net/ – rbm Mar 29 '16 at 12:55
0

You can do something like

var filters = new List<Func<f_results, bool>>();
if (s2test) filters.Add(x => x.s2 == myvar);
if (s3test) filters.Add(x => x.s3 == myvar2);
test.Where (item => filters.All(item));

you can add filters as many as you like, in various conditions, it depends on how dynamic but you can do

if (s2test & s2equals) filters.Add(x => x.s2 == myvar) else if (!s2equals) filters.Add(x => x.s2 != myvar)

etc so you can use switches to build your statements up if you needed.

BugFinder
  • 17,474
  • 4
  • 36
  • 51
0

According to this page, you can create a lambda expression from a string by using an open source library called Dynamic Expresso.

var prices = new [] { 5, 8, 6, 2 };

var whereFunction = new Interpreter()
    .ParseAsDelegate<Func<int, bool>>("arg > 5");

var count = prices.Where(whereFunction).Count();
derloopkat
  • 6,232
  • 16
  • 38
  • 45
0

I don't sure I understand what you want but I'll try.

Lets say that your config is like this:

"s2 == TEST1"

in other time its:

"s3 == TEST2"

So you need to create Predict<Test2> and the body will be somthing like this:

var value= item.GetType().GetField("yourConfigField").GetValue(item);
return value == yourConfigValue;

When yourConfigFiled is s2 or s3 and yourConfigValue is TEST1 or TEST2

Dudi Keleti
  • 2,946
  • 18
  • 33