Search code examples
linqenumsc3.js

c# Linq, how return query with ENUM


So from Front-end I get a string "test1,test2,test3" or "test1,test2" etc. (I can change this on number, it is not a problem);

In back-end I have this:

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,
}

public async Task<Car> GetAllCars(string searchString)
{
    switch (searchString)
    {
        case "How create here query guestion whcih inlude ENUMS":
            query = query.Where( inlude x=> a.Color == x.Contains(x.Color))
        break;

        default:
            query = query.Where(at => 
                    at.Id.ToString().Contains(searchString)
                ||
                    at.Name.Contains(searchString));
         break;
    }
}

I want return all query which have the same enums


Solution

  • If you want to return all cars, then you need to change the return type to List and then lets assume that you have some list of cars (List<Car>) and we should pick cars from there by name and color:

    public async Task<List<Car>> GetAllCars(string searchString)
    {
        return cars.Where(x=>x.Name.Contains(searchString) || x.Color.ToString().Contains(searchString).ToList();
    }