我有一个实体模型,其实体为Person
和LogPerson
。它们是相同的,只是LogPerson
有2个附加字段(LogPersonID
,CreateDate
)。如何强制 LogPerson
到Person
,以便我的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
答案 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