SelectMany operator in Linq C#

SelectMany is an important operator in Linq. It takes each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence. You can find out more information about different overload list from the below link.

http://msdn.microsoft.com/en-us/library/system.linq.enumerable.selectmany.aspx

In this post I am going to explain How it can be really useful when you want to find list related to two entities. Practical example will be like a person can have multiple address and if you want to find list of addresses that are used with person and then this SelectMany operator can be really useful.

So for this example First, I have create Address class with Street and Postalvcode property

public class Address
{
    public string Street { get; set; }
    public string PostalCode { get; set; }
}

Now we are going to create Person class which have Name property and A person can have multiple address so address generic list as property like following.

public class Person
{
    public string Name { get; set; }
    public List<Address> Addresses { get; set; }
}

Read more>>

Published Friday, February 01, 2013 12:59 AM by Jalpesh P. Vadgama
Filed under: , , ,

Comments

No Comments