2

I have a IList of Client type. I would need to iterate through it and return an element that matches some condition. I wanted to use "smarter" way than foreach so I tried Single method, however I am not sure why this works and if it can be done different way (I am not that advanced):

private client GetClientByID(short ID)
{
   return this.ListOfClient.Single(c => c.ID == ID);
}

I do not understand the use of lambda expression..I tried an anonymous method but was not able to write it properly.. thanks

Delimitry
  • 2,987
  • 4
  • 30
  • 39
Petr Had
  • 137
  • 1
  • 6
  • `Single` is implemented using `foreach`. Note that `Single` will iterate the entire list and throw an exception if there is not exactly one `Client` matching the criteria. You may want to use `First` or `FirstOrDefault` instead. – Martin Liversage Dec 15 '11 at 11:07

2 Answers2

1

Your code is correct, this lambda expression is basically a method that returns bool (in this specific case). So imagine that for every item in your ListOfClient it will try to execute that method, if it returns true, then it will return the item.

You need to be carefull that by using Single it will fail if there are 0 or more than 1 matches in your List.

If you are sure that there will be only 1 item then it is fine, if not you can use one of the following:

  • List.SingleOrDefault() //returns null if there are 0 items, fails if there are more than 1
  • List.First() //fails if there are 0 items
  • List.FirstOrDefault() //never fails, returns null if there are 0 items
Sebastian Piu
  • 7,838
  • 1
  • 32
  • 50
  • Thanks, is there a way how to write it without lamba? I am quite confused by lamba expression as they have more meanings. And the help says there is Func<> expected – Petr Had Dec 15 '11 at 11:09
  • @Petr, when you write c => c.ID == ID, that is a indeed a `Func`, so that is fine. The meaning a Func<> has is a method that returns something and takes some stuff by parameter – Sebastian Piu Dec 15 '11 at 11:15
  • Thanks, and is it possible to write it without lamba expression? If so, could you show me how? Thank you – Petr Had Dec 15 '11 at 11:18
  • @Petr, Not in this case, sometimes there is a shortcut that would be to write `.Single(MyMethod)` and then you define `public bool MyMethod(Client c)`. In this case you need the other ID to compare, so it doesn't apply – Sebastian Piu Dec 15 '11 at 11:20
  • @Petr see here for more info http://msdn.microsoft.com/en-us/library/bb549151.aspx – Sebastian Piu Dec 15 '11 at 11:22
0

From MSDN

A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

That means your code is equivalent to

private client GetClientByID(short ID)
{
   return this.ListOfClient.Single(delegate(Client c) { return c.ID == ID; });
}

Single is a linq extension method that could be determined as Enumerable.Single Method (IEnumerable, Func) Pay attention at the second parameter

Func<TSource, bool> predicate

From another article of MSDN

Predicate Delegate. Represents the method that defines a set of criteria and determines whether the specified object meets those criteria.

That means it will check the criteria return c.ID == ID; for each element of the collection and return the one that answers the requirements.

PS Be careful about Single method. I'd prefer to use SingleOrDefault or FirstOrDefault depending on the task.

Alex Kovanev
  • 1,858
  • 1
  • 16
  • 29