实体框架获取属性/值对

时间:2010-07-12 10:31:16

标签: wpf linq entity-framework entity

我正在使用现有数据库,利用Entity Framework进行动态查询构建。这是一个关键因素。我不知道在运行之前我将使用哪些类型的对象,因为允许用户使用查询构建器选择他们想要的对象/属性...所以我使用ObjectQuery<EntityObject>。< / p>

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

我需要在结果中将这些属性显示为列,但我不确定如何让Entity执行此操作。目前,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 个答案:

答案 0 :(得分: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
        };
相关问题