linq.SingleOrDefault

Gets the only element in the array. Null is returned if the array contains no or more than one elements.

Overloads

  • Object SingleOrDefault ()
  • Object SingleOrDefault (condition)

Object SingleOrDefault ()

Gets the only element in the array or null if the array contains no or more than one elements
var myArray = [1, 2, 3, 4];
//Returns null
myArray.SingleOrDefault();

myArray = [1];
//Returns 1
myArray.SingleOrDefault();

Object SingleOrDefault (condition)

Gets the only element in the array where the specified condition is met, if the array contains no or more than one elements, null is returned

Example

var myArray = [1,3,4,5];
//Returns 4
myArray.SingleOrDefault("x => x % 2 == 0");

//Returns null
myArray.SingleOrDefault("x => x == 42");

Last edited Sep 24, 2011 at 6:41 AM by mlorbetske, version 2

Comments

No comments yet.