转换列表<a> to List<b> with Linq c#

时间:2017-09-29 09:36:39

标签: c# list linq

How can i convert my list with typ "Person" (created by my database) to typ "myPerson" without getting erros.

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.

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):

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?

or how can i make a "query" that gives can convert from Person to myPerson?

4 个答案:

答案 0 :(得分:2)

var query = from pers in d.Person 
            select new myPerson 
                  {
                     Name = pers.Name,
                     ...
                  } ;

将点(...)替换为您要映射的其他属性。

答案 1 :(得分:2)

每个myPerson实例都可以投放到Person,因为myPerson类继承自Person。但你不能做相反的事情,将Person投射到myPerson,以便孩子的父母。只有当它真的是myPerson时你才能做到这一点,但在这种情况下不是这样,所以演员表失败了。

如果您有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
}

然后你可以使用:

personen = query.ToList();
newPerson = personen.Select(p => new myPerson(p)).ToList();

答案 2 :(得分:0)

无论是否有实体框架,您都不能从一组类转换为子类集合。

您需要创建一个新集合,在这种情况下是一个新的List,并用您创建的新对象填充它。你可以用linq中的select来做到这一点:

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

将遍历所有Person实例,为每个实例创建一个新的myPerson,并将结果转换为List。

不要写....但是你需要根据myPerson的ctor定义,将Person的所有字段复制到myPerson中,例如:

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

或者作为评论,为myPerson制作一个带有Person的ctor,然后在ctor中你可以复制你需要的字段。

答案 3 :(得分:0)

也许this StackOverflow可以解决您的问题?

以下是Botz3000的接受的答案: “

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

如果无法将BaseClass项强制转换为ChildClass,则当前代码会添加null。如果那是你的意图,那就等同于:

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

但我宁愿建议这一点,其中包括类型检查,并会跳过不匹配的项目:

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