代码之家  ›  专栏  ›  技术社区  ›  Mark Bell

如何用LINQ to SQL查询填充对象的子列表属性

  •  0
  • Mark Bell  · 技术社区  · 14 年前

    我正在为一家网上商店创建一个产品列表。这是相当标准的东西,一页产品缩略图,上面有简要的细节、价格和到完整细节的链接。

    我使用的是存储库模式,所以我有一个中央数据存储库,它可以从SQL服务器返回表。为了简洁起见,我删掉了很多代码,但只是为了让你明白:

    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 物体。然而,我 希望 要做的是填充每个对象的 Images 使用单个 Image 对象,其ID从联接的 Image_Product 表。

    最后我列出了 Products 每个都有一个 图像 在其 图像 属性,该属性具有数据库中该产品的图像ID,其中DisplayOrder为0。

    我试过这个投影,我认为它是有意义的:

    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> 但我不知道为什么。

    有两个问题:为什么在第一种情况下不允许这样做,以及 应该 我这样做是为了得到我想要的结果?

    1 回复  |  直到 14 年前
        1
  •  1
  •   danijels    14 年前

    Product IQueryable<Product> pages IEnumerable<NavigationItem> NavigationItem

    IEnumerable<Product> IEnumerable