简体   繁体   English

如何从函数返回具有另一个实体列表作为属性的实体?

[英]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.

相关问题 如何从实体中的某个属性查询平均值并在LINQ中的对象列表中返回它? - How to query the average from a certain property in an Entity and return it in a List of List of objects in LINQ? 如何在实体框架中列出连接的实体属性而不是完整实体? - How to list joined entity property instead of full entity in Entity Framework? 从实体获取实体列表的通用函数 - generic function to get entity list from entity 在 List 类型的主实体的子属性中搜索值<t>在另一个列表中<t>并返回主要实体</t></t> - Search a value in a main-entity's child property of type List<T> in another List<T> and return main entity 如何使用具有特定条件的嵌套实体返回nHibernate实体 - How to return nHibernate entity with nested entity having specific criteria Linq,其中实体属性包含另一个列表 - Linq, where entity property contains another list 如何从Odata V4中的另一个实体返回字段 - How to return a field from another entity in Odata V4 如何使用实体框架从另一个类返回 ID 或全部? - How return ID or all from another class with Entity Framework? EFcore - 列表属性中相同对象/实体的多个未跟踪实例返回关于另一个跟踪实例的错误 - EFcore - multiple untracked instances of same object/entity in a List Property return error about another tracked instance MVC 3维护实体的List &lt;&gt;属性上的订单 - MVC 3 Maintain an Order on a List<> property from an entity
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM