0

我有一个实体模型,其中包含Person和的实体LogPerson。它们是相同的,只是LogPerson有 2 个附加字段 ( LogPersonID, CreateDate)。如何aLogPerson转换为 aPerson以便跟随我的 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
4

2 回答 2

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
于 2012-09-18T16:24:30.167 回答
0

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

IEnumerable<Person> people = ...;

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

当然,这很乏味,所以如果你有大量的字段或许多地方你必须执行这种操作,你可能想研究一个自动映射库,比如AutoMapper

于 2012-09-18T16:05:25.387 回答