我需要通过降序发布日期来订购存储在数据库中的文章,然后在文章后面的前20条记录Id == 100.
这就是我想对Linq做的事情:
IQueryable<Article> articles =
db.Articles
.OrderByDescending(a => a.PublicationDate)
.SkipWhile(a => a.Id != 100)
.Take(20);
Run Code Online (Sandbox Code Playgroud)
但是,这会生成NotSupportedException,因为SkipWhileLinq to Sql不支持(请参阅此处).
一种可能的解决方案是执行查询,然后SkipWhile使用Linq应用于Object:
IEnumerable<ArticleDescriptor> articles =
db.Articles
.OrderByDescending(a => a.PublicationDate)
.ToList()
.SkipWhile(a => a.Article.Id != 100)
.Take(20);
Run Code Online (Sandbox Code Playgroud)
但这意味着我需要首先将整个有序列表加载到内存中,然后在一个文件后加载20篇文章Id == 100.
有没有办法避免这种巨大的内存消耗?
更一般地说,在SQL中实现这一目标的最佳方法是什么?
我有一个大型数据集,我需要在其中的一部分执行复杂的计算.对于我的计算,我需要根据输入参数从大型集合中获取一大块有序数据.
我的方法签名如下所示:
double Process(Entity e, DateTimeOffset? start, DateTimeOffset? end)
Run Code Online (Sandbox Code Playgroud)
我想到以下两种方法:
double result = 0d;
IEnumerable<Quote> items = from item in e.Items
where (!start.HasValue || item.Date >= start.Value)
&& (!end.HasValue || item.Date <= end.Value)
orderby item.Date ascending
select item;
...
return result;
Run Code Online (Sandbox Code Playgroud)
double result = 0d;
IEnumerable<Item> items = e.Items.OrderBy(i => i.Date);
if (start.HasValue)
items = items.SkipWhile(i => i.Date < start.Value);
if (end.HasValue)
items = items.TakeWhile(i => i.Date <= end.Value);
... …Run Code Online (Sandbox Code Playgroud)