高效的 LINQ to Entities 查询案例?
在这种情况下,我需要一些帮助来重构一个好的查询以从 SQL Server 数据库获取数据。
假设您有一个包含 20000 首歌曲的数据库,每首歌曲有 6 个字段,每个字段都是一个类别(例如 Category1 = Dance、Category2 = House、Category3 = Deep、Category4 = Minimal、Category5 = null、Category6 = null)。
当您想要获得 Dance 和 House 中的所有歌曲但不关心所有其他类别时,您会如何处理?或深度和最小,其他一切都不重要?
我可以说问的是哪些类别!
我可以想象一种有很多组合的方法......而且感觉真的很难看......
I have this case where I need some help on refactoring a good query to fetch data from the SQL Server database.
The case is say you have a database with 20000 songs and each song has 6 fields with each field is a category (e.g. Category1 = Dance, Category2 = House, Category3 = Deep, Category4 = Minimal, Category5 = null, Category6 = null).
How would you approach when you want to get all the songs that is in Dance and House but doesn't matter all other categories? or Deep and Minimal and doesn't matter all other?
I can say which categories are asked!
I can imagine an approach where has lots of combinations....and it feels really ugly...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我的例子:
这个解决方案很糟糕。
我认为,你的数据库模式很糟糕。看看如何连接和关联表
和您的 linq 请求:
或动态请求
我不知道代码是否运行... ^^
祝你好运!
My exemple:
This solution is bad.
My opinion, your shema of database is bad, . Look how to join and association table
and your linq request :
or dynamic request
I dont know if the code run ... ^^
Good luck !
您可以在纯 SQL 中使用这样的查询来完成您的任务:
但据我记得 Linq-to-entities 不支持
IN
所以您需要其他东西。另一种半黑客方法应该适用于 EF 4.0(这里我假设
#
是一个永远不会出现在您的类别名称中的符号)最后一个解决方案 - 标准化您的数据库。
You could use query like this in plain SQL to accomplish your task:
but as far as I remember Linq-to-entities doesn't support
IN
so you'll need something else.Another half-hacky approach which should work on EF 4.0 (here I'm assuming that
#
is a symbol which will be never present in your category names)And last solution - normalize your database.