Reputation: 6762
I am trying to write a c# lambda expression with a dynamic where condition and trying to understand the type of content inside where condition. How can I call that content inside where clause dynamically?
I am taking string here for example
string query1 = "p=>p.id == 1 && p.name==\"abc\" ";
string query2 = "p=>p.id == 2 && p.name==\"def\" ";
//Normal Lambda Expression:
var details = _db.MyTable.Where(p=>p.id == 1 && p.name=="abc")
//Trying to check these if it works
var details1 = _db.MyTable.Where(query1)
var details2 = _db.MyTable.Where(query2)
Upvotes: 2
Views: 121
Reputation: 914
Lambda expressions can use variables within their scope, so you can write it as:
int queryId = 1;
string queryName = "abc";
var details = _db.MyTable.Where(p=>p.id == queryId && p.name== queryName);
In this way you could determine what queryID
and queryName
were dynamically and pass them to the lambda expression. I'm also assuming you intended to check for equality with ==
rather than assign with =
.
Upvotes: 2