How to filter a list in C # with lambda expression?

Am I trying to filter a list so that it only lists with a Brisbane suburb?

FROM#

Temp t1 = new Temp() { propertyaddress = "1 russel street", suburb = "brisbane" }; Temp t2 = new Temp() { propertyaddress = "12 bret street", suburb = "sydney" }; List<Temp> tlist = new List<Temp>(); tlist.Add(t1); tlist.Add(t2); List<Temp> tlistFiltered = new List<Temp>(); //tlistFiltered. how to filter this so the result is just the suburbs from brisbane? public class Temp { public string propertyaddress { get; set; } public string suburb { get; set; } } 
+6
source share
1 answer

Use Where to filter the sequence.

  var tlistFiltered = tlist.Where(item => item.suburb == "brisbane") 

LINQ expressions such as Where return IEnumerable<T> . I usually take the result with var, but you can use ToList() to project the result into a list. Just change what you need to do with the list later.

 List<Temp> tlistFiltered = tlist .Where(item => item.suburb == "brisbane") .ToList() 

Please note that with the above, you do not need to highlight a new list. The Where and ToList() methods return a new sequence that you only need to capture using the link.

+19
source

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


All Articles