12

Using C# with LINQ, how can I merge two lists of different objects, say, Seminar and Conference? They have some common and some different fields/properties and do not share unique id.

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

class Conference
{
   Guid confNumber,
   DateTime joinDate
   Type type
}

I have a list of:

List<Seminar>
List<Conference>

I need to merge them into a super List:

List<Object>

A code snippet would be great help.

13
  • 1
    Please clarify merge. Posting the code you tried would also be a good idea. Commented Jul 4, 2014 at 18:27
  • 1
    Yes, a code snippet would definitely be a great help Commented Jul 4, 2014 at 18:29
  • 2
    Your definition of merge is far more important than our definition. If they have common properties, then ideally they share an interface that would facilitate the merge. Commented Jul 4, 2014 at 18:32
  • 1
    What are you trying to accomplish? Why do you want heterogeneous objects in the same collection? I think you have a design problem, you're just not aware of it yet. Commented Jul 4, 2014 at 18:43
  • 1
    @user, excellent. Now please post this information in your question (use the edit link). Again, posting the code you tried would also help, otherwise at this level of complexity your question will be closed. Commented Jul 4, 2014 at 18:51

3 Answers 3

15

If you just want a single List<object> containing all objects from both lists, that's fairly simple:

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

If that's not what you want, then you'll need to define what you mean by "merge".

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks lot! How to cast it back to original object to get -lets say- properties of Seninar and Conference from the combined list, in my case, objectList ?
I tried doing something like following:<br> List<object> combinedList = employee.Seminar.Cast<object>()<br> .Concat(employee.Conference) .ToList(); <br><br> var TotalPoint = combinedList<br> .Count(x => x.Status == Completed<br> && x.EndDate <= DeadLineDate); <br><br> It did not work because x is still object type, it does not know what type it is refering to (Seminar or Conference). How to cast it back to point to a particular object while reading the properties of either Seminar or Conference ?
@user3410713: You'll need to create an interface containing the common properties that you need to read. Have both classes implement that interface, change the .Cast<object>() to .Cast<ISeminarOrConference>(), and replace List<object> with List<ISeminarOrConference>; you'll then be able to access the common properties from the combined list.
6

Following code works fine for me, if this is your definition of Merge

One solution

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());

Where A and B are some classes as follows

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

Another Solution

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());

Where A and B are having class definition as

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

3 Comments

Thanks lot! How to cast it back to original object to get -lets say- properties of Seninar and Conference from the combined list, in my case, allS?
I tried doing something like following:<br> List<object> combinedList = employee.Seminar.Cast<object>()<br> .Concat(employee.Conference) .ToList(); <br><br> var TotalPoint = combinedList<br> .Count(x => x.Status == Completed<br> && x.EndDate <= DeadLineDate); <br><br> It did not work because x is still object type, it does not know what type it is refering to (Seminar or Conference). How to cast it back to point to a particular object while reading the properties of either Seminar or Conference ?
then Another Solution will work for you, apart form that you can also have if (o is Seminar ){} else if(o is Conference){}
1

Simple method of pure code

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();
    }

1 Comment

It's too complicated, I'm looking for a more simple solution

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.