Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.7k views
in Technique[技术] by (71.8m points)

sql server - Query a key/value table as a single row using EntityFramework 6

I'm using MSSQL, EntityFramework 6 and PredicateBuilder to create a dynamic, user controlled query mechanism to pull records from the DB. If you know a way in Entity to do this then great. Otherwise if I can get a way from MSSQL then I'll be a step closer in researching how to do this in EntityFramework.

I've got tables with parent identifier and key/value as the rows. The data looks like this:

ParentKey
ID  ImportIdentifier
1   FFFFF-FFFFF-FFFFF-FFFFF

ProductTable
ID  name       value        fkParentKey
1   Style       Round           1
2   Color       Blue            1
3   Package     Boxed           1
4   ProdName    Hair clip       1

*** The column "name" in this table is not static; it changes. So my original attempt with using MAX kind of worked until I realized that issue.

I'm trying to make the data storage as flexible as possible, but I guess with the flexibility I'm getting a whole another level of complexity with searching for that data.

I can query on one search term ok; it comes back with say rows of "style". But I only care about returning "Name" from the query, but searching on all terms.

So if this was in a single table with each of the above names as a column I would expect something like this:

select ProdName from ProductTable where Style='round' and Color='blue'

or

select ProdName from ProductTable where Package='Boxed' and Color='blue'

Both of which would give my expected result of "Hair clip"

However, since name is in the same table as well as the other keys I don't know how to query this; especially in EntityFramework. And since I'm using PredicateBuilder to make the dynamic query it adds another level of complexity to it.

I found the following SO questions, but they don't fit my scenario:

Linking rows together, not searching: Join a whole table as single row Get query's result as a single row?

I need to be able to search all data for terms as stated above, but I'm only needing the "non-searched" row of "ProdName" returned.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...