2010-07-12 95 views
1

我正在使用现有数据库,利用动态查询生成器的实体框架。这是一个关键因素。我不知道要在运行时使用什么类型的对象,因为用户可以使用查询生成器来选择他们想要的对象/属性......所以我使用的是ObjectQuery<EntityObject>实体框架获取属性/值对

一切都很好。使用正常的引用,使用.Include()和.Select()的组合就可以实现。但是,我有几张桌子,这些桌子被证明是棘手的。这些基本上是属性表,具有属性名称/值对。

我需要这些属性在结果中显示为列,但我不确定如何让实体执行此操作。目前,Entity可以找到它们,但它只是返回与我查询的任何对象相关的属性行列表。

例如...

主表:

customerid1 customerName 
customerid2 customerName2 

属性表:

1 customerid1 attribute1 value1 
1 customerid1 attribute2 value2 
2 customerid2 attribute2 value3 

最后,我需要显示:

customer  attribute1 attribute2 
------------------------------------ 
customername1 value1  value2 
customername2    value3 

权现在,我得到更多的东西:

customer  attributes 
------------------------------------- 
customername1 attributeitemlistobject 
customername2 attributeitemlistobject 

任何意见将不胜感激。

回答

1

听起来好像要沿着线的东西:

var q = from c in Context.Customers 
     let attribute1 = c.Attributes.FirstOrDefault(a => a.Name.Equals("attribute1") 
     let attribute2 = c.Attributes.FirstOrDefault(a => a.Name.Equals("attribute2") 
     select new 
     { 
      customer = c.Name, 
      attribute1 = attribute1, 
      attribute2 = attribute2 
     };