使用 Linq 获取摘要和计数
我有一个 SQL Server 数据库,其中包含一个事件表,其中存储了大约一百万个事件,每个事件都有一个 dateStart、dateEnd、标题、评级和其他位。
我需要显示一个年份列表,其中每年显示 5 个评分最高的事件以及该年的事件总数。
所以,像...
2009 年 5 大活动(来自 199 项活动)
- 活动A
- 活动B
- 事件C
- 活动 D
- 事件E2010 年 5 大活动(来自 469 项活动)
- 活动F
- 活动G
- 活动H
- 活动一
- 事件 J...等等
由于记录数量巨大,我想避免从数据库中提取所有内容的 Linq 查询,但我不知道这是否可能,而且我的 Linq 知识是还不足以弄清楚这是如何运作的。
从数据库检索此数据结构的最有效方法是什么?
非常感谢,我一整天都在绞尽脑汁试图解决这个问题。
I have a SQL Server database with an Events table that stores around a million events, each with a dateStart, dateEnd, title, rating and other bits.
I need to display a list of years, where each year displays the 5 highest rated events and the total count of events in that year.
So, something like...
Top 5 events for 2009 (from 199 events)
- Event A
- Event B
- Event C
- Event D
- Event ETop 5 events for 2010 (from 469 events)
- Event F
- Event G
- Event H
- Event I
- Event J.... etc.
Because of the sheer quantity of records, I'd like to avoid a Linq query that will pull everything out of the database, but I don't know if that is possible and my Linq knowledge is not enough to work out how this would work.
What is the most efficient method for retrieving this data structure from the database?
Thanks a lot in advance - been mangling my brain all day trying to work it out.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,我们假设您只关心开始日期。你想要这样的东西:
坦率地说,我不知道会产生什么样的 SQL,或者它是否会非常有效 - 你应该找出生成的 SQL,然后在 SQL 探查器中查看查询执行计划是。
Well, let's assume that you only care about the start date. You'd want something like:
Frankly I have no idea what kind of SQL that will produce, or if it'll be even vaguely efficient - you should find out the SQL generated, and then look in the SQL profiler to see that the query execution plan is.
你必须使用 ORM,有几种可能,包括(但不限于)Linq-To-Sql、Entity Framework、NHibernate(我不确定这里是否支持 LINQ)等。
所有这些都将翻译LINQ 查询运算符将在数据库中执行 SQL 查询,并且仅返回您需要或请求的信息。
You will have to use an ORM, there are several possibilites, including (but not limited to) Linq-To-Sql, Entity Framework, NHibernate (I'm not sure about LINQ support here), etc.
All of those will translate the LINQ query operators to an sql query that will be executed in the database, and will only return the information you need or requested.