开发者

Entity Framework getting attribute/value pairs

开发者 https://www.devze.com 2023-01-07 10:40 出处:网络
I\'m working with an existing database, utilizing the Entity Framework for dynamic query builder. This is a key factor here. I don\'t know what types of objects I\'ll be working with until runtime, as

I'm working with an existing database, utilizing the Entity Framework for dynamic query builder. This is a key factor here. I don't know what types of objects I'll be working with until runtime, as the user is allowed to select which objects/properties they want with the query builder... So I'm using ObjectQuery<EntityObject>.

Everything works great. With normal references, using a combination of .Include() and .Select() does the trick. However, I've got a few tables which are proving to be tricky. These are basically property tables, with an attribute name/value pair.

I need these attributes to be displayed as columns in my result, but I'm not sure how to get Entity to do this. At the moment, Entity can find them, but it just returns a list of the attribute lines relevant to whatever object I'm querying against.

For开发者_如何学Python example...

Main Table:

customerid1 customerName
customerid2 customerName2

Attribute Table:

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

In the end, I need to display:

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

Right now, I get something more like:

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

Any suggestions would be greatly appreciated.


Sounds like you want something along the lines of:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号