Wednesday, May 18, 2011

If you want to return a query with Default if its Empty?

If you want to return a query with default value if it is empty you can go with DefaultIfEmpty

void Main()
{
Coder coder = new Coder("Jon");

List coderCollection = new List()
{
new Coder("Anish"),
new Coder("marokey"),
};

var result = coderCollection.Where(r => r.Name == "Pat").DefaultIfEmpty(coder);

}

// Define other methods and classes here
class Coder
{
public string Name;

public Coder(string Name)
{
this.Name = Name;
}
}

No comments:

Post a Comment