簡體   English   中英

如何使用LINQ To SQL查詢填充對象的子級List屬性

[英]How can I populate an object's child List property with a LINQ To SQL query

我正在為在線商店創建產品清單。 這是非常標准的東西,頁面上有產品簡介的縮略圖,簡短的詳細信息,價格以及完整詳細信息的鏈接。

我使用的是存儲庫模式,因此我有一個中央數據存儲庫,該存儲庫為我提供了來自SQL Server的表。 為了簡潔起見,我已經剪掉了很多代碼,但是這樣您就會明白:

public class SqlProductsRepository : IProductsRepository
{
    private Table<Product> productsTable;

    public SqlProductsRepository(string connectionString)
    {
        var context = new DataContext(connectionString);

        productsTable = context.GetTable<Product>();
        // More tables set up here
    }

    public IQueryable<Product> Products
    {
        get { return productsTable; }
    }

    // More properties here
}

我將以下對象映射到表:

[Table(Name = "Products")]
public class Product
{
    [Column(IsPrimaryKey = true)]
    public string ProductCode { get; set; }
    [Column]
    public string Name { get; set; }
    [Column]
    public decimal Price { get; set; }

    public List<ShopImage> Images = new List<ShopImage>();
}

[Table(Name = "Images_Products")]
public class Image_Product
{
    [Column] 
    public int ImageID { get; set; }
    [Column] 
    public string ProductCode { get; set; }
    [Column] 
    public int DisplayOrder { get; set; }
}

[Table(Name = "Images")]
public class Image
{
    [Column(Name = "ImageID")]
    public int ImageID { get; set; }
    [Column]
    public bool Caption { get; set; }
}

如果我執行以下查詢:

// 'db' is the repository (member variable of the controller class)

IQueryable<Product> products = from p in db.Products
                               join ip in db.Image_Product on p.ProductCode equals ip.ProductCode
                               where ip.DisplayOrder == 0
                               select p;

我得到一個很好的IQueryable Product對象。 但是,我想做的是用單個Image對象填充每個對象的Images列表屬性,並從聯接的Image_Product表中設置其ID。

因此,我最終獲得了一個Products列表,每個列表在其Images屬性中都有一個Image ,該列表在DisplayOrder為0的數據庫中具有該產品的圖像ID。

我嘗試了這個投影,我認為這是合理的:

IQueryable<Product> products = from p in db.Products
                               join ip in db.Image_Product on p.ProductCode equals ip.ProductCode
                               where ip.DisplayOrder == 0
                               select new Product { 
                                    ProductCode = p.ProductCode,
                                    Price = p.Price,
                                    Images = new List<Image> { 
                                        new Image { ImageID = ip.ImageID } 
                                    }
                               };

哪個會編譯,但會引發運行時錯誤: Explicit construction of entity type 'xxx.Product' in query is not allowed.

在項目的其他地方,我這樣做:

var pages = from i in db.TemplatePageNavigationItems
            orderby i.DisplayOrder
            select new NavigationItem {
                ID = i.PageID,
                ParentID = i.ParentID,
                Text = i.Name,
                Title = i.Name,
                Url = (i.Folder == null) ? "" : i.Folder
            };

並沒有任何投訴! 我認為這與第一個返回IQueryable<Product>查詢有關,但我不確定為什么。

確實有兩個問題:為什么在第一種情況下不允許這樣做?為了獲得理想的結果,我應該怎么做?

如錯誤所述,您不能在查詢中構造顯式實體類型( Product就是該類型),該實體類型應返回IQueryable<Product> 您的pages查詢將返回IEnumerable<NavigationItem>並且NavigationItem似乎不是數據庫中定義的實體類型。

如果您需要投影對象的明確的,定制的實例,則可以嘗試在第一個查詢中返回IEnumerable<Product>或定義一個單獨的類型,然后返回該類型的IEnumerable

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM