Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      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()));
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading