【问题标题】:How can i write a linq query for multiple tables in entity model MVC4?如何为实体模型 MVC4 中的多个表编写 linq 查询?
【发布时间】:2013-04-05 19:21:50
【问题描述】:

我的实体模型中有这些表

dbo.Options

[OptionsId] [OptionName]
    1        Color
    2        Size
    3        Fit

dbo.ProductAttributes:

[ProdcutAttributeId]   [SKU]       [OptionId]   [Value]
     3001              Shirt_1001    1            Grey
     3002              Shirt_1001    2              S 
     3003              Shirt_1001    3           Regular
     3004              Shirt_1002    1            Black
     3005              Shirt_1002    2             M
     3006              Shirt_1002    3            Slim

dbo.Products:

[ProductId  [ProductName]   [ProductDescription]    [Stock] [ImageURL]  [ProductType]

dbo.ProductSKU:

   ID      SKU       ProductId  Price   ImageURL
   2001    Shirt_1001   1001    12  C:\Users\Administrator\Desktop\Images\LongSleeveShirt.jpg
   2002    Shirt_1002   1001    13  C:\Users\Administrator\Desktop\Images\LongSleeveShirtBlack.jpg
   2003    Shirt_1003   1001    12  C:\Users\Administrator\Desktop\Images\LongSleeveShirtkhaki.jpg
   2004    Shirt_1004   1001    13  C:\Users\Administrator\Desktop\Images\LongSleeveShirtOrange.jpg
   2005    Tshirt_1001  1002    13  C:\Users\Administrator\Desktop\Images\PlainWhiteT.jpg
   2006    Tshirt_1002  1002    12  C:\Users\Administrator\Desktop\Images\PrintedTBlue.jpg
   2007    Tshirt_1003  1002    13  C:\Users\Administrator\Desktop\Images\PrintedTWhite.jpg
   2008    Tshirt_1004  1002    12  C:\Users\Administrator\Desktop\Images\LongSleeveBlackT.jpg

所以这些是我在实体模型中的表,现在我想编写一个 linq 查询,我将在其中将 productID 作为参数传递,我应该将 productidproductid,productname,productdescription 作为参数传递,并且该产品的每个Sku 都有sku 的每个选项和SKU 的imageURl

如何为此编写 linq 查询?

这就是我的模型的样子

                 public class ProductItems
                  {
                   public long ProductID { get; set; }
                   public string ProductName { get; set; }
                   public string ProductDescription { get; set; }
                   public string ImageURL { get; set; }
                   public string SKU { get; set; }
                   public long OptionID { get; set; }
                   public string optionName { get; set; }
                   public string Value{ get; set; }
                   public ProductItems()
                    {
                     if (SKUs == null )
                      SKUs = new List<productSKU>();
                     }
                   public List<productSKU> SKUs { get; set; }     
                   public List<options> oPTIONS { get; set; }
                   }

                 public class productSKU
                   {
                  public productSKU()
                  {
                  if (oPTIONS == null)                          
                      oPTIONS = new List<options>();
                    }
                     public string productsku { get; set;}
                    public string SKUImageURL { get; set;}
                    public List<options> oPTIONS { get; set; }
                    }

                 public class options
                    {
                     public long OptionID { get; set; }
                     public string OptionName { get; set;}
                     public string OptionValue { get; set;}
                    }

现在我想将prodcuname,descriptionprodid 绑定到ProductItems 类,每个sku 详细信息都应该绑定到ProductSku 类,每个SKuOption 应该绑定到选项类

我最后应该得到这些列 [ProductID ],[ProductName ],[ProductDescription ],[SKU],[OptionId],[OptionName],[Value],[ImageURL] 最后我需要的是 ProductName,Description 产品的每个 SKU,每个选项值和产品名称

这是我想要做的事情,但我很困惑如何将它绑定到我的模型

                    var produ = from PS in products.ProductSKUs
                    join PA in products.ProductAttributes on PS.SKU equals PA.SKU
                    join p in products.Products on PS.ProductId equals p.ProductId
                    join o in products.Options on PA.OptionId equals o.OptionsId
                    where PS.ProductId==ID
                    select new ProductItems()
                          {
                              ProductID=p.ProductId,
                              ProductName=p.ProductName,
                              ProductDescription = p.ProductDescription,

我被困在这里下一步该怎么做,任何人都可以在这里帮助我或提供解决方案将不胜感激

【问题讨论】:

    标签: linq asp.net-mvc-4 entity-framework-4 asp.net-web-api


    【解决方案1】:

    感谢任何试图回答我的问题的人,我为此努力并得到了解决方案 任何想要参考这个的人我都在这里发布解决方案

            private ProductEntities products = new ProductEntities();
    
        public IEnumerable<ProductItems> GetProductDetail(long ID)
        {       
            ProductItems items=new ProductItems();
            List<ProductItems> objcollection=new List<ProductItems>();   
            var productdetail=from prod in products.Products where prod.ProductId==ID select new {productid=prod.ProductId,description=prod.ProductDescription,productname=prod.ProductName};           
            foreach(var detail in productdetail)
            {
             items.ProductID=detail.productid;
             items.ProductName=detail.productname;
             items.ProductDescription=detail.description;                    
             var prodsku=from prodksu in products.ProductSKUs where prodksu.ProductId==ID select prodksu;           
            foreach(var sku in prodsku)
            {
                productSKU prdsk=new productSKU();
                prdsk.productsku=sku.SKU;
                prdsk.SKUImageURL=sku.ImageURL;
                items.SKUs.Add(prdsk);
                var opt = from PA in products.ProductAttributes
                          join O in products.Options on PA.OptionId equals O.OptionsId
                          where PA.SKU == prdsk.productsku
                          select new { OptionId = PA.OptionId, OptionValue = PA.Value, OptionName = O.OptionName }; 
                foreach(var op in opt)
                {
                    options obj=new options();
                    obj.OptionID=Convert.ToInt16(op.OptionId);
                    obj.OptionName = op.OptionName;
                    obj.OptionValue=op.OptionValue;
                    prdsk.oPTIONS.Add(obj); 
                }            
                objcollection.Add(items);
            } 
            }      
    
                return objcollection;         
        }
    

    【讨论】:

      猜你喜欢
      • 2015-06-24
      • 2018-02-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 2021-05-10
      • 2021-01-15
      • 1970-01-01
      相关资源
      最近更新 更多