[英]How to return entity having a List of another entity as a property from function?
How to write a SQL function which would return a class (entity) which has a property which is a List of another class entity using the Entity Framework ? 如何使用实体框架编写一个SQL函数,该函数将返回一个类(实体),该类(实体)的属性是另一个类实体的列表?
I have gone through TVF in EF and this is the closest to what I am trying to achieve except that I have a view model wherein there is a List<entity2>
. 我已经通过EF中的TVF,这与我要达到的目标最接近,除了我的视图模型中存在
List<entity2>
。
How do I write a robust function that would return the kind of resultset which will be favourable to the view model? 我如何编写一个健壮的函数,该函数将返回对视图模型有利的结果集?
Some code 一些代码
public class Entity1
{
public List <Entity2> extra { get; set; }
public string img { get; set; }
}
public class Entity2
{
public string itemType { get; set; }
public int Quantity { get; set; }
}
I want to create a view with the view model Entity1
. 我想用视图模型
Entity1
创建一个视图。 How can I do this? 我怎样才能做到这一点?
You can pass your view model to view like this.First create an object of view model and fill it with data. 您可以像这样将视图模型传递给视图。首先创建一个视图模型对象,并用数据填充它。
DBContext db=new DBContext();
Entity ent=new Entity();
ent.extra=db.Entity2.ToList();//This will get all data from db for entity2 table
ent.img="some value";
return return View(ent);
And in view you will do like this. 鉴于您将这样做。 So you will have data in it
这样您就会有数据
@model Entity
Replace name of DbContext if required. 如果需要,请替换DbContext的名称。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.