简体   繁体   中英

Querying multiple data using System.Linq

I am new to.Net Framework and am no expert in any programming language. I am just learning new things.

I want to get a list of objects from my database with Ids of 2, 6, and 9 and also all objects with Ids greater than 20. Here is my code.

int [] toRemove= { 2, 6, 9 };
var contributions = dbContext.FoodMenu
    .Where(x => toRemove.Contains(x.Id))
    .ToList();

As you see, the query with all Ids that are greater than 20 is not yet included. Is there a way I can add to the list with a single query only? How can I do that? Thank you so much.

Add || or condition for Id is greater than 20.

The query will fetch either the Ids within (2, 6, 9) or greater than 20.

int [] toRemove= { 2, 6, 9 };
var contributions = dbContext.FoodMenu
    .Where(x => toRemove.Contains(x.Id)
        || x.Id > 20)
    .ToList();

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