Merge lists into one

2019-03-04 17:17发布

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# Joins/Where with Linq and Lambda

I have a class like this

public class Person
{
  public int Id{get;set;}
  public string Name{get;set;}
  public string MailingAddress{get;set;}
}

I have a method like below

public IList<Person> GetNames(IList<int> ids)

This will give me List of persons like below

1 "Sam" ""

2 "Dev" ""

4 "Hummy"

I have another method like below

 public IList<Person> GetMailingAddress(IList<int> ids)

This will give me List of persons like below

1 "" "ABC"

6 "" "TTT"

2 "" "XYZ"

Now I need to merge results of two methods so that I can have my final result like this

1 "Sam" "ABC"

2 "Dev" "XYZ"

UPDATE : I am sorry I didnot clearly give my test data. Please see above my test data

4条回答
等我变得足够好
2楼-- · 2019-03-04 17:26

I'm slightly confused by what your methods are returning, if you need to combine the two results to get full Person objects then there are two ways you might be able to get things working.

  1. If you can rely on the same number of objects being returned in the same order, you can try:

    names.Zip(mailingAddresses, (n, m) => new Person
    {
        Id = n.Id,
        Name = n.Name,
        MailingAddress = m.MailingAddress
    });
    
  2. If you can't rely on both of those conditions, you can use a Join:

    names.Join(mailingAddresses, n => n.Id, m => m.Id, (n, m) => new Person
    {
        Id = n.Id,
        Name = n.Name,
        MailingAddress = m.MailingAddress
    });
    

Even though you have those two options, there's a third and better option if you have control over the code that actually gets the objects from the data source. If you know you need those two pieces of data, you should create a single method that queries the datasource a single time to get all of the data rather than querying once per piece of data.

查看更多
可以哭但决不认输i
3楼-- · 2019-03-04 17:39

You can solve it with Enumerable.Zip and Ordering the Data before:

IEnumerable<Person> list = GetNames(new List<int>()).OrderBy(p => p.Id).Zip(GetMainlingAddress(new List<int>()).OrderBy(p => p.Id), (first, second) => { return new Person() { Id = first.Id, Name = first.Name, MailingAddress = second.MailingAddress }; });
查看更多
男人必须洒脱
4楼-- · 2019-03-04 17:42

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.

查看更多
三岁会撩人
5楼-- · 2019-03-04 17:50

Enumerable.Zip definitely will solve your issue.

查看更多
登录 后发表回答