How to pass an IQueryable type as a query to a linq

2 hours ago 1
ARTICLE AD BOX

I want to pass the Query part of the below Linq as a param. So that I can decide what query I may need an pass as a param.

for example based on the state value passed from the API requet, it can be either new, Progress, Done or just null. When null, add no where condition.

public async Task<litEditJob> GetData(int LitEditJobId) { var litEditJob = await context.LitEditJob .Include(x => x.LitEdit) .FirstAsync(x => x.JobId == LitEditJobId); var LitEditTasksQuery = context.Entry(LitEditJob) .Collection(b => b.LitEditJobTasks) .Query(); await LitEditTasksQuery .Where(x => x.State == "new") .OrderBy(x => x.Id) .Skip(10) .Take(10) .LoadAsync(); return litEditJob; }

I tried a few things, such as

IQueryable<BulkEditJobTask> query = new List<LitEditJobTask>().AsQueryable(); //I'll add some switch case condition query = query.Where(t => t.State == LitEditStatus.Progress).OrderBy(x => x.Id).Skip(skip).Take(size);

passing query as a param to the method. But nothing seems to work. Is there a way to do so??

public async Task<litEditJob> GetData(int LitEditJobId, IQueryable<LitEditJobTask> query) { query = context.BulkEditJobTask.AsQueryable().Concat(query); var litEditJob = await context.LitEditJob .Include(x => x.LitEdit) .FirstAsync(x => x.JobId == LitEditJobId); var LitEditTasksQuery = context.Entry(LitEditJob) .Collection(b => b.LitEditJobTasks) .Query(); if (query is not null) { LitEditTasksQuery = query; } await LitEditTasksQuery.LoadAsync(cancellationToken: cancellationToken); return litEditJob; }

I am not sure what is the right way to do this. Is there a way I can pass the query as a param then execute the Linq.

Read Entire Article