且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

演员/转换IEnumerable的< T>到了IEnumerable< U> ;?

更新时间:2023-11-11 23:09:40

您可以不投,因为它们是不同的类型。你有两个选择:

You can't cast because they are different types. You have two choices:

1)改变类,这样PersonWithAge从人继承

1) Change the class so that PersonWithAge inherits from person.

class PersonWithAge : Person
{
        public int Age { get; set; }
}



2)创建新的对象:

2) Create new objects:

IEnumerable<Person> p = pwa.Select(p => new Person { Id = p.Id, Name = p.Name });