Merge lists into one

后端 未结 4 524
我寻月下人不归
我寻月下人不归 2021-01-28 10:57

I saw posts like below which are really hard for me to understand. So I am re-posting it. Sorry if someone feels it\'s duplicate. I have just simple requirements

C# Join

4条回答
  •  日久生厌
    2021-01-28 11:41

    Perform join on those two methods returning values by Lambda style Linq syntax:

    var query = GetNames().Join(GetMailingAddress(),
                                        n => n.Id,
                                        e => e.Id,
                                        (n, e) => new { n.Id,n.Name,e.Email});
    
            foreach (var item in query)
            {
                Console.WriteLine(item.Id + "-" + item.Name +"-"+ item.Email);
            }
    

    Perform join on those two methods returning values by Sql-style Linq syntax:

    var query = from n in GetNames()
                join e in GetMailingAddress()
                on n.Id equals e.Id
                select new {n.Id,n.Name,e.Email };
    foreach (var item in query)
        {
            Console.WriteLine(item.Id + "-" + item.Name +"-"+ item.Email);
        }
    

    Note:Where GetName() and GetMailingAddress() method returns list of result set.

提交回复
热议问题