开发者

how to merge / join two different list into 1

开发者 https://www.devze.com 2023-04-01 22:40 出处:网络
I am working on an application for Windows Phone platform. Run into a question. I\'d 2 different List, 1 is direct read from xml file, and the other by some calculation. And I want to merge this two

I am working on an application for Windows Phone platform. Run into a question.

I'd 2 different List, 1 is direct read from xml file, and the other by some calculation. And I want to merge this two lists into 1, so I can display it out.

List 1:

public class studentClass1
{
    public string studentID { get; set; }
    public string studentFirstName { get; set; }
    public string studentLastName { get; set; }
}

List 2:

public class studentClass2
{
    public string studentID { get; set; }
    public string studentGradePoint { get; set; }
}

First of all, I had readout the studentClass1 via

var studentList= from query in studentIndex.Descendants("student")
                             select new driversClass
StudentList1 = studentList.ToList();

Secondly, I process the student Grade Point calculation on the function and output to the 2nd list :

studentClass2 SG = new studentClass2
            {
                studentID = thestudentID ,
                studentGradePoint  = thestudentGradePoint .ToString()
            };
开发者_开发百科
            StudentList2.Add(SG);

studentListAll = StudentList1 + StudentList2

now, I want to join this two list together so that I can output to screen by calling

studentResultListBox.Itemsource = StudentListAll;

any suggestion the code how would look like? Thanks.


Assuming you just want to combine the appropriate info from both lists (it is not totally clear from your question) - introduce a third class studentClass3 that holds all the properties you want and use a join to match instances with a matching studentID:

var studentList3 = (from s1 in studentList1
                    join s2 in studentList2 on s1.studentID equals s2.studentID
                    select new studentClass3()
                    {
                        studentFirstName = s1.studentFirstName,
                        studentID = s1.studentID,
                        studentGradePoint = s2.studentGradePoint,
                        studentLastName = s1.studentLastName
                    }).ToList();

In general this problem should be rather solved when you read in the XML than trying to combine the lists later on - having three different classes for students might be confusing. Also take a look at the recommended naming conventions, they are a little off.


Use interface, e.g.

interface IStudent
{
    string studentID { get; }

    // other common properties below
}

public class StudentClass1 : IStudent
{
    public string studentID { get; set; }
    public string studentFirstName { get; set; }
    public string studentLastName { get; set; }
}

public class StudentClass2 : IStudent
{
    public string studentID { get; set; }
    public string studentGradePoint { get; set; }
}

and then

    studentResultListBox.Itemsource = list1.Cast<IStudent>()
                                           .Concat(list2.Cast<IStudent>())
                                           .ToList();

Or, if the inheritance is not the case, just cast everything to System.Object (+ override object.ToString)

 studentResultListBox.Itemsource = list1.Cast<object>()
                                        .Concat(list2.Cast<object>())
                                        .ToList();
0

精彩评论

暂无评论...
验证码 换一张
取 消