3

I have the following: List<InputRow> which contains a number of InputRow objects.

I am wondering if there is a way for me to use a lambda function on my original list to give me a new List where InputRow.someProperty > 1 for all the objects.

This would leave me with a list of InputRow objects all having someProperty greater than 1.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
Baxter
  • 5,633
  • 24
  • 69
  • 105
  • possible duplicate of [using LINQ to remove objects within a List](http://stackoverflow.com/questions/853526/using-linq-to-remove-objects-within-a-listt) – Muad'Dib Apr 23 '12 at 19:18

4 Answers4

7

You can of course also do this:

var list = new List<string>(){ "a", "b", "c" };

list.RemoveAll(s => s == "b");

which removes the items in place instead of creating a new list.

Phil
  • 42,255
  • 9
  • 100
  • 100
6

You could use LINQ (a conjunction of the .Where() and .ToList() extension methods):

List<InputRow> originalList = ...
List<InputRow> filteredList = originalList
    .Where(x => x.someProperty > 1)
    .ToList();
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
3

Sure. You can do this:

var newList = inputRowList.Where(inputRow => inputRow.someProperty > 1).ToList();
Smur
  • 3,075
  • 7
  • 28
  • 46
2
List<InputRow> newlist = oldlist.Where(x => x.someProperty > 1).ToList();

This will search your old list on the condition that someProperty > 1 and convert the result into List using .ToList()

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208