[英]Best practice to merge Table Entity with View Entity
Is there any better way to merge a Entity Table with a Entity View 有没有更好的方法来合并实体表和实体视图
A little example: I have a Person table with: 一个小例子:我有一个Person表,其中:
id , 编号
name , 名字 ,
lastname 姓
columns and a view named ViewPersonLastLocations with: 列和名为ViewPersonLastLocations的视图, 其中包含:
person_id 为person_id
location_name . location_name 。
I need to display Person table with the information of ViewPersonLastLocations . 我需要显示带有ViewPersonLastLocations信息的Person表。 Actually i can "merge" those entities with two foreachs, and i create a variable in the Person partial class .
实际上,我可以通过两个foreach来“合并”这些实体,并且可以在Person局部类中创建一个变量。
Is there any other way to do this? 还有其他方法吗?
I am a little unclear on what you want based on your last comment, but will start with this code for a join if the relationship is 1:1. 我根据您的最新评论不清楚您想要什么,但是如果关系为1:1,则将从此代码开始进行联接。 If it is 1:Many, then it is similar, but project into a collection.
如果为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();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.