0

我想做一个简单的查询来检索特定文章的组件数据+语言数据,然后将其放入视图模型中。

ProductComponent表是Product的子表,其中的相关字段是 ComponentID 和 ProductId(外键,parentId),所以我想将 ProductComponents 链接到 Product 和 ProductTranslations 我拥有所有语言特定数据的地方,所以我尝试了在一个查询中完成所有操作,以检索特定产品的组件列表。

这是查询:

    public IEnumerable<ProductComponents> ListComponents(int productid, string language)
    {
        var query = (from c in context.ProductComponents

                     from ct in context.ProductTranslations
                     where ct.ProductId == c.ComponentId
                     where ct.ProductLanguage == language

                     from cp in context.Product
                     where cp.ProductId == c.ComponentId

                     where c.ProductId == productid

                     select new EnumComponents
                     {
                         ProductId = c.ComponentId,
                         Name = ct.ProductName,
                         SKU = cp.SKU
                     });

        return query;
    }

这给出了这个错误,并突出显示返回查询;部分:

Cannot implicitly convert type 'System.Linq.IQueryable<Artikelhantering.Models.EnumComponents>' to 'System.Collections.Generic.IEnumerable<Artikelhantering.Models.ProductComponents>'. An explicit conversion exists (are you missing a cast?)

这是大部分数据模型,根据我在谷歌上搜索和查看 Stack Overflow 的结果,表之间的关系可能是罪魁祸首,所以我包括了大部分。

public class Product
    {
        [Key]
        public int ProductId { get; set; }
        [DisplayName("Article nr")]
        public string SKU { get; set; }
        [DisplayName("Product Category")]
        public int ProductCategoriesId { get; set; } 
        [DisplayName("Alternative Category")]
        public int? AdditionalCategoriesId { get; set; }
        [DisplayName("Show purchase button?")]
        public bool Purchase { get; set; } 
        [DisplayName("Show Product?")]
        public bool ShowProduct { get; set; } 
        [DisplayName("Picture name")]
        public string Picture { get; set; } 
        [DisplayName("Is reference product?")]
        public bool Reference { get; set; } 
        [DisplayName("Inprice")]
        public decimal inPrice { get; set; }    
        [DisplayName("Additional costs")]
        public decimal AddCost { get; set; } /
        [DisplayName("Weight in kg")]
        public decimal Weight { get; set; }
        [DisplayName("Volume in m^3")]
        public decimal Volume { get; set; }
        [DisplayName("Vat code, use 0")]
        public decimal VAT { get; set; }
        public virtual IList<ProductTranslations> ProductTranslations { get; set; }
        public virtual IList<ProductPrices> ProductPrices { get; set; }
        public virtual IList<ProductComponents> ProductComponents { get; set; }
        public virtual IList<ProductAccessories> ProductAccessories { get; set; }
        public virtual ProductCategories ProductCategories { get; set; }
        public virtual ProductCampaigns ProductCampaigns { get; set; }        
    }

    public class ProductTranslations 
    {
        [Key]
        public int ProductTranslationsId { get; set; }
        public int ProductId { get; set; } // This one links to the Product class
        [DisplayName("Language Code")]
        public string ProductLanguage { get; set; }
        [DisplayName("Description")]
        public string Description { get; set; }
        [DisplayName("Product Name")]
        public string ProductName { get; set; }
        [MaxLength(255)]
        [DisplayName("Meta Keywords")]
        public string MetaKeywords { get; set; }
        [MaxLength(255)]
        [DisplayName("Meta Description")]
        public string MetaDescription { get; set; }
        public virtual Product Product { get; set; }
    }

    public class ProductComponents 
    {
        [Key]
        public int ProductComponentsId { get; set; }
        public int ProductId { get; set; }
        public int ComponentId { get; set; }
        public virtual IList<ProductTranslations> ProductTranslations { get; set; }
        public virtual Product Product { get; set; }
    }   

然后我像这样定义模型之间的关系:

 protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

                modelBuilder.Entity<ProductCategories>()
                .HasMany(x => x.ProductCategoriesTranslations) // Categories has many Translations
                .WithRequired(y => y.ProductCategories)     // Translations require a category
                .HasForeignKey(p => p.ProductCategoriesId);

                modelBuilder.Entity<Product>()
                .HasMany(x => x.ProductPrices) // Product has many ProductPricings
                .WithRequired(y => y.Product)     // ProductPricing has required Product
                .HasForeignKey(p => p.ProductId);

                modelBuilder.Entity<Product>()
                .HasMany(x => x.ProductTranslations) // Product has many ProductTranslations
                .WithRequired(y => y.Product)     // ProductTranslations has required Product
                .HasForeignKey(p => p.ProductId);

                modelBuilder.Entity<Product>()
                .HasMany(x => x.ProductComponents) // Product has many ProductComponents
                .WithRequired(y => y.Product)     // ProductComponents has required Product
                .HasForeignKey(p => p.ProductId);

                modelBuilder.Entity<Product>()
                .HasMany(x => x.ProductAccessories) // Product has many ProductAccessories
                .WithRequired(y => y.Product)     // ProductAccessories has required Product
                .HasForeignKey(p => p.ProductId);

            }

我猜我需要为 ProductComponents 与 ProductTranslations 和 Product 定义适当的关系,但我不太确定如何,我尝试了各种方法来创建 ProductComponents -> ProductTranslations 之间的关系,但没有任何成功。当然,问题很可能是其他问题。

4

1 回答 1

0

看来是我自己想出来的。现在它终于点击了,非常简单和明显的错误。关键是将public IEnumerable<ListComponents>重命名为 public IEnumerable<EnumComponents>

回想起来似乎是合乎逻辑的,我应该在这里使用视图模型而不是模型,因为这毕竟是我想要填充的内容,然后我可以从视图模型内部调用模型就好了。

public IEnumerable<EnumComponents> ListComponents(int productid, string language)
    {
        //return context.ProductComponents.Where(m => m.ProductId == productid);
        var query = (from c in context.ProductComponents
        where c.ProductId == productid
        select new EnumComponents
        {
        Name = c.ProductTranslations
                    .Where(i => i.ProductLanguage == language)
                    .Where(i => i.ProductId == c.ComponentId)
                    .Select(i => i.ProductName)
                    .Single(),
        SKU = c.Product.SKU,
        ProductId = c.ComponentId
        });

        return (query);
    }
于 2013-04-19T09:51:55.597 回答