Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
246 views
Welcome To Ask or Share your Answers For Others

1 Answer

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".


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...