Turn off statement in compiled query?

Is there any way to use something like a switch statement in a compiled query for linq for / sql objects? For example, when returning sorted records from a database, I would like to use a switch statement in one compiled query to sort by different properties, instead of writing 2 compiled queries (ascending and descending) for each property that you might want sort, which can contain up to 10 compiled queries, even for a simple sortable grid.

In T-SQL, this would be easy with the case argument, since I would suggest that such a design is supported on most databases, so why it will not be supported on the linq / entity infrastructure.

Any solution for this?

+4
source share
1 answer

As kyndigs refers in his comment, the only solution is to fake the switch statement with a tertiary statement ? : ::

  internal static readonly Func<MyEntities, long, MessageSortField, int, int, IQueryable<Model.Message>> MessagesPagedSortedAscQuery = CompiledQuery.Compile((MyEntities db, long folderId, MessageSortField sortField, int skip, int take) => ( sortField == MessageSortField.Date ? db.Messages.Where(e => e.FolderId == folderId).OrderBy(m => m.Date).Skip(skip).Take(take) : sortField == MessageSortField.Subject ? db.Messages.Where(e => e.FolderId == folderId).OrderBy(m => m.Subject).Skip(skip).Take(take) : sortField == MessageSortField.From ? db.Messages.Where(e => e.FolderId == folderId).OrderBy(m => m.From).Skip(skip).Take(take) : db.Messages.Where(e => e.FolderId == folderId).Skip(skip).Take(take) )); 
+3
source

Source: https://habr.com/ru/post/1311623/


All Articles