高效的 LINQ to Entities 查询案例?

发布于 2024-10-26 02:24:32 字数 355 浏览 0 评论 0原文

在这种情况下,我需要一些帮助来重构一个好的查询以从 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

独自唱情﹋歌 2024-11-02 02:24:32

我的例子:

(from song in SongRepository
where song.Cateroy1 == Dance || song.Cateroy2 == Dance || song.Cateroy3 == Dance || song.Cateroy4 == Dance || song.Cateroy5 == Dance
select song)
.Union(from song in SongRepository
where song.Cateroy1 == House || song.Cateroy2 == House || song.Cateroy3 == House || song.Cateroy4 == House || song.Cateroy5 == House
select song)

这个解决方案很糟糕。
我认为,你的数据库模式很糟糕。看看如何连接和关联表

table Song
SongID
SongTitle
SongCategoryID

table Category
CategoryID
CategoryTitle

table SongCategory
SongCategoryID
SongID
CategoryID

和您的 linq 请求:

SongRepository.Where(song => song.SongCategory.Category.CategoryTitle = "Dance" 
|| song.SongCategory.Category.CategoryTitle = "House").Select(song =>song); 

或动态请求

  var query = SongRepository.Where(song => song.SongCategory.Category.CategoryTitle = "Dance").Select(song =>song); 

 query = query.Union(SongRepository.Where(song => song.SongCategory.Category.CategoryTitle = "House").Select(song =>song));

我不知道代码是否运行... ^^

祝你好运!

My exemple:

(from song in SongRepository
where song.Cateroy1 == Dance || song.Cateroy2 == Dance || song.Cateroy3 == Dance || song.Cateroy4 == Dance || song.Cateroy5 == Dance
select song)
.Union(from song in SongRepository
where song.Cateroy1 == House || song.Cateroy2 == House || song.Cateroy3 == House || song.Cateroy4 == House || song.Cateroy5 == House
select song)

This solution is bad.
My opinion, your shema of database is bad, . Look how to join and association table

table Song
SongID
SongTitle
SongCategoryID

table Category
CategoryID
CategoryTitle

table SongCategory
SongCategoryID
SongID
CategoryID

and your linq request :

SongRepository.Where(song => song.SongCategory.Category.CategoryTitle = "Dance" 
|| song.SongCategory.Category.CategoryTitle = "House").Select(song =>song); 

or dynamic request

  var query = SongRepository.Where(song => song.SongCategory.Category.CategoryTitle = "Dance").Select(song =>song); 

 query = query.Union(SongRepository.Where(song => song.SongCategory.Category.CategoryTitle = "House").Select(song =>song));

I dont know if the code run ... ^^

Good luck !

依 靠 2024-11-02 02:24:32

您可以在纯 SQL 中使用这样的查询来完成您的任务:

SELECT * FROM Songs
WHERE 'Dance' IN (Category1,Category2,Category3,Category4,Category5,Category6)
AND 'House'   IN (Category1,Category2,Category3,Category4,Category5,Category6)

但据我记得 Linq-to-entities 不支持 IN 所以您需要其他东西。


另一种半黑客方法应该适用于 EF 4.0(这里我假设 # 是一个永远不会出现在您的类别名称中的符号)

var result = Songs
   .Select(s => new {Name = s.Name, SearchString = '#'+s.Category1+'#'+s.Category2+'#'+...+'#'})
   .Where(p => p.SearchString.Contains("#Dance#") && p.SearchString.Contains("#House#"))
   .Select(p => p.Name);

最后一个解决方案 - 标准化您的数据库。

You could use query like this in plain SQL to accomplish your task:

SELECT * FROM Songs
WHERE 'Dance' IN (Category1,Category2,Category3,Category4,Category5,Category6)
AND 'House'   IN (Category1,Category2,Category3,Category4,Category5,Category6)

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)

var result = Songs
   .Select(s => new {Name = s.Name, SearchString = '#'+s.Category1+'#'+s.Category2+'#'+...+'#'})
   .Where(p => p.SearchString.Contains("#Dance#") && p.SearchString.Contains("#House#"))
   .Select(p => p.Name);

And last solution - normalize your database.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文