[LINQ] Select the difference of SelectMany

Select () and SelectMany () work is based on a source value generating one or more result values.
Select () value for each source generates a result value. Thus, the overall result is a set of sources having the same number of elements of the collection. In contrast, SelectMany () to generate a single overall result, which comprises a series of subset values from each source. SelectMany passed as a parameter to the () conversion function must return a value enumerable sequence value for each source. Then, SelectMany () will enumerable sequences of these series to create a larger sequence.

private class NamedEntity
{
    public NamedEntity(int id, string name) { this.ID = id; this.Name = name; }
    public int ID { get; set; }
    public string Name { get; set; }
}

public Window()
{
    InitializeComponent();

    NamedEntity[] list1 = { new NamedEntity(1, "Albert"), new NamedEntity(2, "Burke"), new NamedEntity(3, "Connor") };
    NamedEntity[] list2 = { new NamedEntity(2, "Albert was here"), new NamedEntity(3, "Burke slept late"), new NamedEntity(4, "Happy") };
    IList<NamedEntity[]> l = new List<NamedEntity[]>() { list1, list2 };
    IList<NamedEntity> tokens = l.SelectMany(a => a).ToList();
    var grp = tokens.GroupBy(a => a.ID);
    IList<NamedEntity> result = grp.SelectMany(a => a.Take(1)).ToList();
}

 

Guess you like

Origin www.cnblogs.com/chriskwok/p/11609515.html