List<int> list = new List<int> { 1, 2, 4, 5, 6, 7 };
string selectQuery = "x";
var result = from x in list
where x > 3
select selectQuery;
This code will return "xxxx", I want it to return 4 5 6 7.
I want to enter the select dynamically by a string but this code returns the string. I got a table of conditions, for each condition I want to use a LINQ Query, so I put the select statement in a string table and I want to apply it for each one.
CodePudding user response:
This is because you are selecting (returning) the selectQuery
variable for each item.
Try this:
var result = from x in list
where x > 3
select x;
This will return each item of the original list that matches the condition in the where clause.
CodePudding user response:
Try this: Just convert your select query string into a integer, and filter the list with that value:
List<int> list = new List<int> { 1 ,2 ,4 ,5 ,6 ,7 };
string selectQuery = "2";
var result = from x in list
where x>Convert.ToInt32(selectQuery)
select x;
//other way can be
list.Where(x=>x>Convert.ToInt32(selectQuery)).ToList()
With this way you can do the query with a dinamic value in the string select query.