简体   繁体   中英

Entity Framework Core create query using ifs

Something like this:

        Itens = db.Transacao.AsNoTracking()
            if (x == 1)
                .Where(w => w.Confirmado == true)
            else
                .Where(w => w.Data.Date >= DateTime.Now.Date)
            .Include(i => i.Pessoa)
            .Include(i => i.Categoria)
            .ToList();

I know that this don't exist, but, exist something similar to this?

Well, you can do something like this:

IQueryable<[your entity here]> query = db.Transacao.AsNoTracking();
if (x == 1)
    query = query.Where(w => w.Confirmado == true);
else
    query = query.Where(w => w.Data.Date >= DateTime.Now.Date);

return query.Include(i => i.Pessoa)
            .Include(i => i.Categoria)
            .ToList();

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM