Home > Blockchain >  Querying multiple data using System.Linq
Querying multiple data using System.Linq

Time:01-25

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.

CodePudding user response:

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();
  • Related