C#LINQ SQL EntityFramework挑战

时间:2014-01-28 10:46:25

标签: c# linq entity-framework

我有(在此上下文中)3个包含产品,属性和属性类型的表。 在其中一个前端案例中,我需要根据以前选择的不同属性类型的值来获取不同属性的列表。

例如; 所有产品都有3种属性类型'width','height'和'diameter'。 根据选择,我需要返回类型'width','height'和'diameter'的属性列表。我选择了一个宽度,比方说100,我想抓住属性类型'height'的所有属性,其中'width'属性为100的产品。

解释起来有点棘手,但我希望这有点清楚。

产品:

CREATE TABLE [dbo].[Product](
    [ProductId] [int] IDENTITY(1,1) PRIMARY KEY,
    [SupplierId] [int] NULL,
    [BrandId] [int] NULL,
    [ProductDiscountGroupId] [int] NULL,
    [VATProductPostingGroupId] [int] NULL,
    [PromotionTypeId] [int] NULL,
    [LocationId] [int] NULL,
    [Name] [nvarchar](250) NOT NULL,
    [Description2] [nvarchar](1000) NULL,
    [Description3] [nvarchar](4000) NULL,
    [Description4] [nvarchar](4000) NULL,
    ...
    )

属性:

CREATE TABLE [dbo].[ProductProperty](
    [ProductPropertyId] [int] IDENTITY(1,1) NOT NULL,
    [ProductPropertyTypeId] [int] NOT NULL,
    [ProductId] [int] NOT NULL,
    [ProductPropertyLookupId] [int] NULL,
    [Unit] [varchar](50) NULL,
    [DecimalValue] [decimal](18, 2) NULL,
    [BooleanValue] [bit] NULL,
    [TextValue] [varchar](100) NULL,
    ...
    )

物业类型:

CREATE TABLE [dbo].[ProductPropertyType](
    [ProductPropertyTypeId] [int] IDENTITY(1,1) NOT NULL,
    [Priority] [int] NOT NULL,
    [UOM] [varchar](50) NULL,
    [Name] [varchar](50) NOT NULL,
    [PropertyType] [int] NOT NULL,
    [Description] [varchar](50) NULL,
    [Visible] [bit] NOT NULL,
    ...
    )

1 个答案:

答案 0 :(得分:0)

您是否尝试选择给定高度/宽度的所有属性?

int creteria = 100 


var = from ptype in ProductPropertyType 
      join prop in ProductProperty on ptype.ProductPropertyId  = prop.ProductPropertyId
      where (prop.Unit == creteria && ptype.Name == "Height") || (prop.Unit == creteria && ptype.Name == "Width") 
      Select prop 

我没有测试过linq会100%工作但这是你追求的那种类型吗?

编辑1

你好不确定你是否已经解决了这个问题,但是我又在Linq做了一次。可能有更好的方法来做到这一点,我不能保证这会给你你的确切输出。让我们知道这是否适合你。

    var AllWidthProperties = from p in ProductProperty
                             where p.Name == "Width"
                             select p;

    var AllProducstWtihHeightOfValue = from p in ProductProperty 
                                       where p.Name == "Height" && p.Unit == creteria
                                       join Prod in Product on Prod.ProductId equals p.ProductId
                                       group Prod by Prod.ProductId into g
                                       select new
                                       {
                                           ProductId = g.Key
                                       };

    var SelectedWithProperties = from p in AllWidthProperties 
                                 join Prod in AllProducstWtihHeightOfValue on Prod.ProductId equals p.ProductId
                                 select p;