1
var orderedStudents = students.OrderBy(x => x.FacultyNumber).ToList();
var orderedWorkers = workers.OrderByDescending(x => x.moneyPerHour).ToList();

Those are the two lists that I want to merge, but I can't figure out how since they have different types.

4
  • Why would you want to merge them? What manipulation or data extraction do you want to do on them? Commented Nov 24, 2015 at 20:44
  • Do they have a common base class? Commented Nov 24, 2015 at 20:44
  • Those are two classes with name, lastName and few more properties. I need to put them in one list and sort them by NAME. Commented Nov 24, 2015 at 20:45
  • 1
    Since the main point of contention is that they are different types, you should update the question to at least show the types of the things. Commented Nov 24, 2015 at 20:50

2 Answers 2

5

Either derive them from common base class and then you would use sth like

var humanBeings = orderedStudents.Cast<Human>().Concat(orderedWorkers.Cast<Human>()).ToList();

or .. cast them to common base class :) object

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

Comments

-1

Another option : Create a class that contains the common properties

class Person {
    public string Name { get; set; }
    public string Lastname { get; set; }
}

var orderedStudents = students.OrderBy(x => x.FacultyNumber).Select(x => new Person { Name = x.Name, Lastname = x.Lastname});
var orderedWorkers = workers.OrderByDescending(x => x.moneyPerHour).Select(x => new Person { Name = x.Name, Lastname = x.Lastname});
var mergedList = orderedStudents.Concat(orderedWorkers).ToList();

3 Comments

Probably would be better to use base class instead of manual copying.(if possible, ofc) More OOP-ish I would say.
As with most things in software development, it depends. If you have a base class or interface with the required properties, or you can add it then yes. If not then this is just another option ;)
Yes, sure. Sometimes even adding a single field is a huge pain :) Not even speaking about changing model hierarchy

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.