我试图创建一个新查询,然后根据某些条件进行编辑并添加到其中。我不知道该怎么做写在方括号[]
中的功能/* Create the query */
var query = from quote in Query()
where quote.documentNum = document_Input
select quote;
// Now change the sort field
query = filter.sort == SortFied.quote ? [alter the query function to sort by documentNum<string>] : [alter the query function to sort by enteredData<DateTime?>]
// Now change the sort direction
query = filter.sortDir == SortDirection.Ascending ? [alter the query to sort in an Ascending order] : [alter the query to sort in an Descending order]
这些只是之后必须添加的两个条件。之后添加它的目的是避免每种可能的条件重复编写代码。
答案 0 :(得分:0)
我正在假设这样的事情。
var query = Query().Where(x => x.documentNum == document_Input);
query = filter.sortDir == SortDirection.Ascending ? query.OrderBy(x => filter.sort == SortFied.quote ? x.documentNum : x.enteredData) : query.OrderByDescending(x => filter.sort == SortFied.quote ? x.documentNum : x.enteredData);
您可以使用委托进一步减少代码重复。