Looking for a JavaEE Architect or Potential Tech Co-Founder?
Please don't hesitate to contact me.

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 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 + "}");
Using IN like operator in Entity Framework Using IN like operator in Entity Framework Reviewed by Edward Legaspi on Friday, January 21, 2011 Rating: 5

No comments:

Powered by Blogger.