如何合并两个不同对象的列表?

时间:2014-07-04 18:25:28

标签: c# linq

在LINQ中使用C#,如何合并两个不同对象列表,比如研讨会和会议? 它们有一些共同的和一些不同的字段/属性,并且不共享唯一的id。

class Seminar
{
   int id,
   DateTime joinDate,
   string name
}

class Conference
{
   Guid confNumber,
   DateTime joinDate
   Type type
}

我有一个清单:

List<Seminar>
List<Conference>

我需要将它们合并到一个超级List

List<Object>

代码段会很有帮助。

3 个答案:

答案 0 :(得分:4)

如果您只想要一个包含两个列表中所有对象的List<object>,那就非常简单了:

List<object> objectList = seminarList.Cast<object>()
    .Concat(conferenceList)
    .ToList();

如果那个不是你想要什么,那么你需要通过“合并”来定义你的意思。

答案 1 :(得分:3)

以下代码适用于我,如果这是您对Merge的定义

一个解决方案

List<A> someAs = new List<A>() { new A(), new A() };
List<B> someBs = new List<B>() { new B(), new B { something = new A() } };

List<Object> allS = (from x in someAs select (Object)x).ToList();
allS.AddRange((from x in someBs select (Object)x).ToList());

其中A和B是如下的某些类

class A
{
    public string someAnotherThing { get; set; }
}
class B
{
    public A something { get; set; }
}

另一种解决方案

List<A> someAs = new List<A>() { new A(), new A() };
List<B> someBs = new List<B>() { new B(), new B { something = string.Empty } };

List<Object> allS = (from x in someAs select (Object)new { someAnotherThing = x.someAnotherThing, something = string.Empty }).ToList();
allS.AddRange((from x in someBs select (Object)new { someAnotherThing = string.Empty, something = x.something}).ToList());

其中A和B的类定义为

class A
{
    public string someAnotherThing { get; set; }
}
class B
{
    public string something { get; set; }
}

答案 2 :(得分:0)

纯代码的简单方法

internal class Person
{
    public int Id { get; set; }
    public string UserName { get; set; }
}

internal class User
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

internal class UserPerson
{
    public int Id { get; set; }
    public string UserName { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

private static void Main(string[] args)
    {
        Person[] people = new Person[3] { new Person { Id = 1, UserName = "AliUserName" }, new Person { Id = 2, UserName = "MortezaUserName" }, new Person { Id = 3, UserName = "SalarUserName" } };
        User[] users = new User[4] { new User { FirstName = "ali", LastName = "Barzegari" }, new User { FirstName = "Morteza", LastName = "Sefidi" }, new User { FirstName = "Salar", LastName = "Pirzadeh" }, new User { FirstName = "Babak", LastName = "Hasani" } };

        UserPerson[] userPeople = new UserPerson[people.Length > users.Length ? people.Length : users.Length];
        if (people.Length > users.Length)
            for (int i = 0; i < people.Length; i++)
            {
                userPeople[i] = new UserPerson
                {
                    Id = people[i].Id,
                    UserName = people[i].UserName,
                    FirstName = users.Length <= i ? "" : users[i].FirstName,
                    LastName = users.Length <= i ? "" : users[i].LastName
                };
            }
        else
            for (int i = 0; i < users.Length; i++)
            {
                userPeople[i] = new UserPerson
                {
                    Id = people.Length <= i ? 0 : people[i].Id,
                    UserName = people.Length <= i ? "" : people[i].UserName,
                    FirstName = users[i].FirstName,
                    LastName = users[i].LastName
                };
            }
        Console.ReadLine();
    }