Created
September 7, 2014 09:43
-
-
Save foyzulkarim/8fed29730847dc6917fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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