I want to implement a filter on multiple columns, but I don't want to write for every column a new query. So I implemented a GetDistinctProperty function which looks like this :
public ActionResult GetDistinctProperty(string propertyName)
{
var selector = CreateExpression<TDomain>(propertyName);
var query = this.InventoryService.GetAll(Deal);
var results = query.Select(selector).Distinct().ToList();
return Json(results, JsonRequestBehavior.AllowGet);
}
private static Expression<Func<T, object>> CreateExpression<T>(string propertyName)
{
// Specify the type we are accessing the member from
var param = Expression.Parameter(typeof(T), "x");
Expression body = param;
// Loop through members in specified property name
foreach (var member in propertyName.Split('.'))
{
// Access each individual property
body = Expression.Property(body, member);
}
var conversion = Expression.Convert(body, typeof(object));
// Create a lambda of this MemberExpression
return Expression.Lambda<Func<T, object>>(conversion, param);
}
Let's take as example that I have as propertyName SiteIdentifier.
The selector gives me as value
{x => Convert(x.SiteIdentifier)}
and when I want to see the results it gives me the following error :
Unable to cast the type 'System.String' to type 'System.Object'.
LINQ to Entities only supports casting EDM primitive or enumeration types.
When I try the select as follow :
var results = query.Select(x=>x.SiteIdentifier).Distinct().ToList();
it works.
Anyone any Idea?