是否有更好的方法将实体表与 Entity View
合并。一个示例:我有一个 person with:
ID ,
名称,
lastName
列和名为 viewPersonLastLocations 的视图with:
person_id
location_name 。
我需要显示 person 具有 viewPersonLastLocations 的信息。实际上,我可以" MERGE" 那些具有两个foreach的实体,我在 person partial class 中创建一个变量。
还有其他方法可以做吗?
我有点不清楚您想要的基于您的上一个评论,但是如果关系为1:1,则从此代码开始以进行加入。如果是1:很多,那是相似的,但是将其投影到一个集合中。
var personWithLocation = context.Persons
.SelectMany(p => context.ViewPersonLastLocations
.Where(vp => vp.person_id == p.id)
.DefaultIfEmpty(),
(p, vp) => new PersonViewModel // create a viewmodel for results or anonymous
{
Id = p.id,
Name = p.name,
LastName = p.lastname,
LocationName = vp.location_name
}
).ToList();