Home > Mobile >  How to use Linq in C# to do this?
How to use Linq in C# to do this?

Time:01-03

I have this Object:

class Car 
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Color Color { get; set; }
}

public enum Color
{
    Red = 1,
    Blue = 2,
    Pink = 3,
    Orange = 4,
}
    

How to create a linq query if I want take objects which have Red and Blue values:

query = query.Where(at => at.Color == Color.Red   Color.Blue);

CodePudding user response:

Either you can make the query with || or operator

query = query.Where(at => at.Color == Color.Red 
    || at.Color == Color.Blue);

Or create an Color array to check whether the value is within the array.

query = query.Where(at => (new Color[] { Color.Red, Color.Blue }).Contains(at.Color));

CodePudding user response:

query.Where(at => at.Color == Color.Red || at.Color == Color.Blue);

CodePudding user response:

        List<Car> cars = new List<Car>();
        ...
        var result = cars.Where(c=>c.Color== Color.Red || c.Color == Color.Blue).ToList(); 
  • Related