Sunday, August 7, 2011

elementary LINQ and Lambdas stuff

  • Get the earliest date from a series of child objects:

    var foo = (from bar in this.CollectionOfChildren select bar.BirthDate).Min();


     
  • Get the last date:

    var foo = (from bar in this.CollectionOfChildren select bar.BirthDate).Max();


     
  • A simple Lambda:

    var foo = this.CollectionOfChildren.Where(bar => bar.IsOk == true);


     
  • In this example, index is the place in the array or list:

    var baz = this.CollectionOfChildren.Where((qux, index) => qux.Age < index);


     
  • Remove duplicates:

    List<T> cleanList = listWithSomeDuplicates.Distinct().ToList();


     

No comments:

Post a Comment