Retrieve list from object in Database

不问归期 提交于 2019-12-11 11:49:40

问题


I want to send a list of names contained in a Database using asp.net

These are my two objects:

public class Shop
{
    public int ID { get; set; }
    public string Country { get; set; }
    public List<Item> Items{ get; set; }
}

public class Item
{
    public int ID { get; set; }
    public string Name { get; set; }
}

I want to set a get controller in order to retrieve a list of items. I did something like this:

 public IEnumerable<Item> Get(int id)
    {

        var items= new List<Item>();
        var shop= new Shop();

        using (var systemDB = new ShopsDB())
        {
                            it = systemDB.Shops.Where(s => s.ID == id).FirstOrDefault<Shop>();
                            items = it.Items;
        }   

            return items;

    }

This return <ArrayOfItem i:nil="true"/>. I want to get the complete list of Items for one shop (e.g. shop with ID=1)


回答1:


This will return you the list

using (var systemDB = new ShopsDB())
    {
                        lab = systemDB.Shops.Where(s => s.ID == id).ToList();
                        items = lab.Items;
    }   



回答2:


I solved modifying the Item object:

public class Shop
{
    public int ID { get; set; }
    public string Country { get; set; }
    public List<Item> Items{ get; set; }
}

public class Item
{
    public int ID { get; set; }
    public string Name { get; set; }
    public int ShopID { get; set; }
}

And set a query to select the ShopID



来源:https://stackoverflow.com/questions/34492175/retrieve-list-from-object-in-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!