no

Using in Like Operator in Entity Framework

Entity Framework is a great advancement to sql unfortunately some old keywords are not supported. But reading the documentation and google y...

Entity Framework is a great advancement to sql unfortunately some old keywords are not supported. But reading the documentation and google you will find a lot of work arounds. Like for example in the case of IN operator.

Old
select * from table where field in (list);

For example in my recent requirement, I have a comma-delimited string: "A,B,C".


IQueryable<MyModel> iQueryable = null;
var temp = "";
var plainString = "A,B,C";
if (plainString.Length > 1)
{ 
  //split "A,B,C" becomes an array of "A", "B", "C"
  var ids = plainString.TrimEnd(',').Split(','); 
  //join the array and add single quotes "'A','B','C'"
  temp = string.Join("','", ids);
  temp = "'" + temp + "'";
}
else
{
  temp = "'" + plainString + "'";
}
iQueryable = _myEntities.MyModel.Where("it.MyFieldHere IN {" + temp + "}");

Related

c# 4297464894683594577

Post a Comment Default Comments

Outsourcing

Are you looking for freelancers in the Philippines? Get in touch.

Donations

If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.

item