Using LINQ, how can I filter a collection on a partial string property?

I have a collection of Car objects,

IEnumerable<Car>

And I want to return a filtered collection of automobile objects based on partial string matching (it is not necessary to start), where the Car.Name property has a specific text in it.

Is this possible with LINQ?

+3
source share
4 answers
from c in cars
where c.Name.Contains("certain text")
select c

or

cars.Where(c => c.Name.Contains("certain text"))
+6
source
IEnumerable<Car> cars = ...
var filteredCars = cars.Where(car => car.Name.Contains("your text"));
+1
source

Contains:

var cars = new List<Car>(); //Or whatever makes sense.

var filteredCars = cars.Where(c => c.Name.Contains("searchstring"));
+1

, :

var filteredCars = cars.Where(car => car.Name.Contains("Fiesta"));

, .

0

Source: https://habr.com/ru/post/1793580/


All Articles