Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merge two lists objects

Tags:

c#

i'm having a problem with merging two list of object here they are:

first one
List<NSKData> NSKDataList = new List<NSKData>();

public class NSKData
{
    public string ID { get; set; }
    public string Issue { get; set; }
    public string ToolTipInfoText { get; set; }
    public NSKData() { }
    public NSKData(string id, string issue, string tooltipinfo)
    {
        ID = id;
        Issue= issue;
        ToolTipInfoText = tooltipinfo;
    }
}

 second one
 List<IssuesMoreInfo> IssuesMoreInfoList = new List<IssuesMoreInfo>();

public class IssuesMoreInfo
{
    public string ID { get; set; }
    public string IssueMoreInfoText { get; set; }
}

If i can get all needed data at one time, i wont ask this question, but i'm grabbing all data for fist one, and only after i can get data for second one.

So, what i need in result is: to get IssueMoreInfo from second one, according to ID in both, like for id 10 in first one, we getting IssueMoreInfoText column in second one and pass it to firt one list in column ToolTipInfoText

Hope for your help, guys, thanks

like image 694
GGodlike Avatar asked Jan 08 '16 08:01

GGodlike


People also ask

How do you merge two lists of objects in python?

One simple and popular way to merge(join) two lists in Python is using the in-built append() method of python. The append() method in python adds a single item to the existing list. It doesn't return a new list of items. Instead, it modifies the original list by adding the item to the end of the list.

Can you concatenate lists in Java?

util. ArrayList appends all of the elements in the specified collection to the end of this list, in the order that they are returned by the specified collection's Iterator. Using this method you can concatenate two lists.

How do I combine two lists in schemes?

The append function joins two lists together to make one. The append function is built into Scheme. It concatenates two lists, that is to say, given two lists list1 and list2 it produces a new list which starts with the same elements as list1 and finishes with those of list2 .


2 Answers

I assume you're looking for Enumerable.Join:

var query = from data in NSKDataList 
            join info in IssuesMoreInfoList 
            on data.ID equals info.ID
            select new NSKData(data.ID, data.Issue, info.IssueMoreInfoText);
NSKDataList = query.ToList();

Another way which does not need to re-create all objects and the list:

var infoIdLookup = IssuesMoreInfoList.ToLookup(i => i.ID);
foreach(NSKData data in NSKDataList)
{
    data.ToolTipInfoText = infoIdLookup[data.ID]
       .Select(i => i.IssueMoreInfoText)
       .FirstOrDefault();
}
like image 172
Tim Schmelter Avatar answered Sep 19 '22 03:09

Tim Schmelter


Try using Linq Join

var query = from c in NSKDataList 
            join o in IssuesMoreInfoList on c.ID equals o.ID
            select new { c.ID, c.Issues , c.ToolTipInfoText , o.IssueMoreInfo  };

The query will contains info from both of the lists

http://www.dotnetperls.com/join

like image 29
User2012384 Avatar answered Sep 20 '22 03:09

User2012384