Altering Query expression post creation

33 views Asked by At

I am trying to create a new query, then based on certain conditions edit and add to it. I do not know how to do the functions written in brackets []

/* 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]

These are just two conditions having to be added afterwards.The aim of adding it afterwards is to avoid duplicate code for each possible condition.

1

There are 1 answers

2
SILENT On BEST ANSWER

I'm assuming something like this.

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);

You could further reduce code duplication by using delegates.