Search code examples
c#asp.netlinq

LINQ comparing string


Question: I want to use LINQ and check if one of the CarNames is inside result1. Why result1 returns nothing but result2 works fine?

result2 returns nothing

IQueryable<Cars_Model> Query = await _services.GetAllCars();
string result1 = "Car,Truck,Van";  
Query = Query.Where(x => x.CarsName.ToUpper().Contains(result1.ToUpper()));

result2 works fine

      IQueryable<Cars_Model> Query = await _services.GetAllCars();
      string result2 = "Car";  // this works fine 
      Query = Query.Where(x => x.CarsName.ToUpper().Contains(result2.ToUpper()));

other information

public class Cars_Model
{
    public Int64 Id { get; set; }
    
    public string CarsName{ get; set; }
}

Solution

  • Just judging by your data I think you intended to do this instead:

    IQueryable<Cars_Model> Query = await _services.GetAllCars();
    var expectedTypes = "Car,Truck,Van".ToUpper().Split(",");  
    Query = Query.Where(x => expectedTypes.Contains(x.CarsName.ToUpper()));