Iqueryable EntityFramework和Linq的每个元素
我目前正在查询数据库中的表格,其中我需要检索24个均匀间隔的数据点,以在图上绘制从数据集中的某个日期到当前时间的黄金价格。该表可以包含数百万行数据。
我想使用模块化(%),但是用LINQ编写时的查询不能转换为SQL查询。我认为这也是由于可让可让人没有枚举的事实,而linq库也没有必要的手段来执行此操作。
当i .tolist()
orderedgolddata
时,这确实有效。但是,在一个非常大的数据集上,我宁愿写一个查询来做到这一点。
public IEnumerable<HistoricalGoldDatum> GetHistoricGoldData(string userId, DateTime firstDate,
int dataPoints = 24)
{
var orderedGoldData =
DbContext.UserBalanceHistoricalData
.Where(gol => gol.DateCreated > firstDate)
.OrderBy(gol => gol.DateCreated);
var div = orderedGoldData.Count() / dataPoints;
return orderedGoldData.Where((b, i) => i % div == 0);
}
如何在最佳时间复杂性中使用LINQ或标准查询来实现我想实现的目标?
I am currently querying a table in a database where I need to retrieve 24 evenly-spaced datapoints to plot on a graph the price of gold from a certain date in a dataset to the current time. The table could contain millions of rows of data.
I wanted to use modular (%) but the query when written in LINQ cannot be translated to the SQL query. This I believe is also due to the fact that IQueryable does not have an enumerator and the LINQ library does not have the necessary means to do this.
This does work when I .ToList()
the orderedGoldData
. However, on a dataset that is extremely large, I would rather write a query to do this.
public IEnumerable<HistoricalGoldDatum> GetHistoricGoldData(string userId, DateTime firstDate,
int dataPoints = 24)
{
var orderedGoldData =
DbContext.UserBalanceHistoricalData
.Where(gol => gol.DateCreated > firstDate)
.OrderBy(gol => gol.DateCreated);
var div = orderedGoldData.Count() / dataPoints;
return orderedGoldData.Where((b, i) => i % div == 0);
}
How can I achieve what I wanted to achieve above on an IQueryable using LINQ or a standard query in the best time complexity?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,linq到SQL可能不会产生最好的效率查询,因此我会使用标准查询,类似的问题:
In such scenario Linq to SQL might produce not the best efficent query so I would use standard query, something like this: