[英]How can I merge two lists when they have different types?
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. 这些是我要合并的两个列表,但是由于它们具有不同的类型,所以我不知道如何合并。
Either derive them from common base class and then you would use sth like 要么从通用基类派生它们,然后使用sth like
var humanBeings = orderedStudents.Cast<Human>().Concat(orderedWorkers.Cast<Human>()).ToList();
or .. cast them to common base class :) object
或..将它们强制转换为通用基类:)
object
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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.