Skip to content

Instantly share code, notes, and snippets.

@foyzulkarim
Created September 7, 2014 09:43
Show Gist options
  • Save foyzulkarim/8fed29730847dc6917fd to your computer and use it in GitHub Desktop.
Save foyzulkarim/8fed29730847dc6917fd to your computer and use it in GitHub Desktop.
public Response Search(SearchModel search)
{
Dictionary<string, Expression> expressions = new Dictionary<string, Expression>
{
{"Price", (Expression<Func<Phone, double>>) (p => p.Price)},
{"Brand", (Expression<Func<Phone, string>>) (p => p.Name)}
};
BdPhonesDbEntities db = new BdPhonesDbEntities();
IQueryable<Phone> phones = db.Phones.Where(search.GetExpression());
Expression<Func<Phone, double>> keySelector = KeySelector<Func<Phone, double>>(expressions, search.OrderBy.PropertyName);
var result = phones.OrderBySearchModel(keySelector, false);
var list = result.Select(p => new { p.Name, p.Brand, p.Price }).ToList();
return new Response(list);
}
private Expression<T> KeySelector<T>(Dictionary<string, Expression> expressions, string key)
{
return (Expression<T>) expressions[key];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment