2014-01-28 27 views
0

我有(在这种情况下)3个表包含产品,属性和属性类型。 在其中一个前端情况下,我需要根据先前选择的不同属性类型的值来获取不同属性的列表。C#LINQ SQL EntityFramework挑战

例如; 所有产品都有属性类型“宽度”,“高度”和“直径”的3个属性。 基于选择,我需要返回类型“宽度”,“高度”和“直径”的属性列表。我已经选择了一个宽度,比方说100,我想要获取属性类型“高度”的所有属性,其中“宽度”属性为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, 
    ... 
    ) 
+0

你应该尝试给你的问题一个更具描述性的标题 - 也许就像“基于来自另外两个表的数据从表中选择”一样?我不确定我是否理解这个问题。 – Hannele

回答

0

你想选择一个给定的高度/宽度都propertys?

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; 
+0

我试图选择产品的所有宽度属性,这些产品也具有值为x的高度属性。 – Erik