如果我有
public ActionResult ListExpenseItems(long id)
{
IQueryable<I_ITEM> expenseItems = er.GetExpenseItems(id);
return PartialView(expenseItems );
}
和LINQ
public IQueryable<I_ITEM> GetExpenseItems(long id)
{
return from i in db.I_ITEM
where i.ExpenseId == id
orderby i.ExpenseItemId ascending
select i;
}
如果我将一个字符串作为参数传递给LINQ方法,比如说"ExpenseTitle"
,我怎么做OrderBy i.ExpenseTitle
以便orderby总是匹配字符串参数?
这种逻辑..但实际上是正确的:
db.I_ITEM.OrderBy(x => (orderBy == 'date') ? x.Date : (orderBy == 'id') ? x.Id : (orderBy == 'cost') ? x.Cost : x.Id);
答案 0 :(得分:2)