简体   繁体   English

使用Linq c#将List <A>转换为List <B>

[英]Convert List<A> to List<B> with Linq c#

How can i convert my list with typ "Person" (created by my database) to typ "myPerson" without getting erros. 如何将我的列表与典型的“人”(由我的数据库创建)转换为典型的“myPerson”而不会得到错误。

I tried this : 我试过这个:

DataClasses1DataContext d = new 
DataClasses1DataContext(MainWindow.mySqlClass.GetConnection());

var query = from pers in d.Person select pers;

personen = query.ToList();
newPerson = personen.Cast<myPerson>().ToList();

But I only get a System.InvalidCastException . 但我只得到一个System.InvalidCastException

public partial class myPerson : Person
{
    public override string ToString()
    {
        return Name + " " + Nachname;
    }
    public myPerson(System.Data.Linq.EntitySet<Bilder> bilder, string geschlecht, int iD, string nachname, string name)
    {
        Bilder = bilder;
        Geschlecht = geschlecht;
        ID = iD;
        Nachname = nachname;
        Name = name;
    }
}

The Class that Linq created (just the frist rows): Linq创建的类(只是第一行):

public partial class Person : INotifyPropertyChanging, INotifyPropertyChanged
{

    private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);

    private int _ID;

    private string _Nachname;

    private string _Name;

    private string _Geschlecht;

    private EntitySet<Bilder> _Bilder;

anyone has a idea how can i convert a List from typ Person to a List with typ myPerson? 任何人都知道如何将列表从典型人转换为列表与myPerson?

or how can i make a "query" that gives can convert from Person to myPerson? 或者我怎样才能创建一个“查询”,可以从Person转换为myPerson?

Every myPerson instance can be casted to Person because the myPerson class inherits from Person . 每个myPerson实例都可以转换为Person因为myPerson类继承自Person But you can't do the opposite, cast a Person to myPerson so the parent to the child. 但你不能做相反的事情,把一个Person投给myPerson这样孩子的父母就可以了。 You could do that only if it was really a myPerson , but it is not in this case so the cast fails. 只有当它真的是myPerson你才能这样做,但是在这种情况下不是这样,所以演员表失败了。

If you have a Person and you want to convert it to myPerson you should provide a constructor (or factory method) that accepts a Person instance and initializes (or returns) the myPerson instance. 如果你有一个Person并且你想将它转换为myPerson你应该提供一个接受Person实例并初始化(或返回) myPerson实例的构造函数(或工厂方法)。

public partial class myPerson : Person
{
    public myPerson(Person p)
    {
        _ID = p.Id;
        _Nachname = p.Nachname;
        _Name = p.Name;
        _Geschlecht = p.Geschlecht;
        _Bilder = p.Bilder;
    }

    private int _ID;
    private string _Nachname;
    private string _Name;
    private string _Geschlecht;
    private EntitySet<Bilder> _Bilder;
    // add properties
}

Then you can use: 然后你可以使用:

personen = query.ToList();
newPerson = personen.Select(p => new myPerson(p)).ToList();
var query = from pers in d.Person 
            select new myPerson 
                  {
                     Name = pers.Name,
                     ...
                  } ;

Replace the dots (...) with the rest of the properties you want to map. 将点(...)替换为要映射的其余属性。

Irrespective of entity framework or not, you cannot cast from a collection of classes into a collection of subclasses. 无论实体框架与否,您都无法从一组类转换为子类集合。

You need to make a new collection, in this case a new List, and fill it with new objects that you make. 您需要创建一个新集合,在这种情况下是一个新的List,并用您创建的新对象填充它。 You could do that with a select in linq like this: 你可以用linq中的select来做到这一点:

d.Person.Select(x => new myPerson(....)).ToList();

Which would go through all the Person instances, create a new myPerson for each one, and convert the result to a List. 这将遍历所有Person实例,为每个实例创建一个新的myPerson,并将结果转换为List。

Don't write the .... though - you need to do this according to your definition of the ctor of myPerson, copying all the fields of Person into myPerson eg: 不要写....但是 - 你需要根据你对myPerson的ctor的定义来做这个,将Person的所有字段复制到myPerson中,例如:

new myPerson(x.Bilder, x.Geschlecht, .......

Or as commented, make a ctor for myPerson that takes a Person, then inside the ctor you can copy the fields you need. 或者作为评论,为myPerson制作一个带有Person的ctor,然后在ctor中你可以复制你需要的字段。

Perhaps this StackOverflow solves your problem ? 也许这个 StackOverflow解决了你的问题?

Here is the accepted answer from Botz3000: " 以下是Botz3000的接受答案 :“

ChildClassList = BaseClassList.Cast<ChildClass>().ToList();

Your current code adds null if a BaseClass item cannot be cast to ChildClass. 如果无法将BaseClass项强制转换为ChildClass,则当前代码将添加null If that was really your intention, this would be equivalent: 如果那是你的意图,那就等同于:

ChildClassList = BaseClassList.Select(x => x as ChildClass).ToList();

But i'd rather suggest this, which includes type checking and will skip items that don't match: 但我宁愿建议这一点,其中包括类型检查,并将跳过不匹配的项目:

ChildClassList = BaseClassList.OfType<ChildClass>().ToList();

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM