Linq to Entities将一个实体转换为类似实体

时间:2012-09-13 20:23:51

标签: vb.net linq entities

我有一个实体模型,其实体为PersonLogPerson。它们是相同的,只是LogPerson有2个附加字段(LogPersonIDCreateDate)。如何强制 LogPersonPerson,以便我的Linq代码后面的VB.NET代码不必尝试使用这两种可能的类型?

例如:

dim p as person
If useLog then
   p = From a In LogPerson Where ID = x
Else
   p = From a In Person Where ID = x
End If

textbox1.text = p.firstname

2 个答案:

答案 0 :(得分:0)

没有任何开箱即用的方法。没有第三方工具/库的最简单方法是使用Select method并手动映射属性,如下所示:

IEnumerable<Person> people = ...;

IEnumerable<LogPerson> logPeople = people.Select(p => new LogPerson { 
    Name = p.Name,
    Age = p.Age,
    ...
});

当然,这很乏味,所以如果您有大量字段或许多地方需要执行此类操作,您可能需要查看自动映射库,例如{{3} }。

答案 1 :(得分:0)

I am WRITING THE SOLUTION IN C#....

person p;
if(uselog)
{
var per = from pobj in LogPerson
          where pobj.ID= x
          select new person{
          firstname = pobj.firstname,
          lastname = pobj.lastname,
          };
}else
{
var per = from pobj in Person
    where pobj.ID= x
    select new person{
    firstname = pobj.firstname,
    lastname = pobj.lastname,
    };
}
p = per.first();
}


textbox1.text = p.firstname