Hi I want to construct a dynamic Entity Framework Linq query with all the properties of an object. Example
I want to :- 1) Object test has 5 public properties. 2) I want to loop through this object and check if each string property is null or empty. 3) If not, I want to write a query that will append a where condition to search the Entity with this value of the property.
public void CheckMyEntity(IQueryable<ABCEty> _allABCs, MyEntity _MyEntityProperty)
{
foreach (var prop in _MyEntityProperty.GetType().GetProperties())
{
if (!String.IsNullOrEmpty(prop.GetValue(_MyEntityProperty,null).ToString()))
{
_allABCs = _allABCs.Where(temp => (temp.ABCMyEntitys.All(MyEntity => MyEntity.MyEntity.<<I cant insert the property here>> == prop.GetValue(_MyEntityProperty,null));
}
}
}
Any help would be very useful! Thanks!
There is a small Dynamic Linq library that does textual evaluation. http://www.hanselman.com/blog/TheWeeklySourceCode48DynamicQueryableMakesCustomLINQExpressionsEasier.aspx
Simply put this class does evaluation of the text and converts it into a Linq expression tree that most LinQ providers like Entity Framework can process.
You can turn each PropertyInfo into a lambda expression and pass that into the query