C# 获取n个没有相关实体或最近没有相关实体的实体
我正在编写一个返回C# 获取n个没有相关实体或最近没有相关实体的实体,c#,entity-framework,linq,asp.net-core,entity-framework-core,C#,Entity Framework,Linq,Asp.net Core,Entity Framework Core,我正在编写一个返回batchSizedto的方法。我想首先用FilterLists填充该批,其中包含一组空的相关Snapshots。如果批处理中还有空间,那么我想包括FilterLists,其中最新的Snapshot(byCreatedDateUtc)是最近的 下面的逻辑工作正常,但我想将GetNeverCapturedList()和GetLeastRecentlyCapturedList()合并到一个EF LINQ查询中。这将避免额外的数据库往返 有办法吗 public class Filte
batchSize
dto的方法。我想首先用FilterList
s填充该批,其中包含一组空的相关Snapshot
s。如果批处理中还有空间,那么我想包括FilterList
s,其中最新的Snapshot
(byCreatedDateUtc
)是最近的
下面的逻辑工作正常,但我想将GetNeverCapturedList()
和GetLeastRecentlyCapturedList()
合并到一个EF LINQ查询中。这将避免额外的数据库往返
有办法吗
public class FilterList
{
public int Id { get; set; }
public ICollection<Snapshot> Snapshots { get; set; }
...
}
public class Snapshot
{
public int Id { get; set; }
public DateTime CreatedDateUtc { get; set; }
public int FilterListId { get; set; }
public FilterList FilterList { get; set; }
...
}
//TODO: combine queries into single query
private async Task<IEnumerable<FilterListViewUrlDto>> GetNextListsToCapture(int batchSize)
{
var neverCapturedLists = await GetNeverCapturedLists(batchSize);
if (neverCapturedLists.Count >= batchSize)
return neverCapturedLists;
var leastRecentlyCapturedLists = await GetLeastRecentlyCapturedLists(batchSize - neverCapturedLists.Count);
return neverCapturedLists.Concat(leastRecentlyCapturedLists);
}
private async Task<List<FilterListViewUrlDto>> GetNeverCapturedLists(int batchSize)
{
return await dbContext.FilterLists
.Where(x => x.Snapshots.Count == 0)
.Take(batchSize)
.ProjectTo<FilterListViewUrlDto>()
.ToListAsync();
}
private async Task<List<FilterListViewUrlDto>> GetLeastRecentlyCapturedLists(int batchSize)
{
return await dbContext.Snapshots
.GroupBy(x => x.FilterList)
.Select(x => x.OrderByDescending(y => y.CreatedDateUtc).First())
.OrderBy(x => x.CreatedDateUtc)
.Select(x => x.FilterList)
.Take(batchSize)
.ProjectTo<FilterListViewUrlDto>()
.ToListAsync();
}
公共类过滤器列表
{
公共int Id{get;set;}
公共ICollection快照{get;set;}
...
}
公共类快照
{
公共int Id{get;set;}
public DateTime CreatedDateUtc{get;set;}
public int FilterListId{get;set;}
公共筛选器列表筛选器列表{get;set;}
...
}
//TODO:将查询合并到单个查询中
专用异步任务GetNextListsToCapture(int batchSize)
{
var NeverCapturedList=等待获取NeverCapturedList(batchSize);
如果(NeverCapturedList.Count>=batchSize)
返回无捕获列表;
var leastRecentlyCapturedList=等待GetLeastRecentlyCapturedList(batchSize-NeverCapturedList.Count);
return NeverCapturedList.Concat(至少百分之三的CapturedList);
}
专用异步任务GetNeverCapturedList(int batchSize)
{
return wait dbContext.FilterLists
.其中(x=>x.Snapshots.Count==0)
.Take(批量大小)
.ProjectTo()
.ToListAsync();
}
专用异步任务GetLeastRecentlyCapturedList(int batchSize)
{
return wait dbContext.Snapshots
.GroupBy(x=>x.FilterList)
.Select(x=>x.OrderByDescending(y=>y.createdDatateutc).First())
.OrderBy(x=>x.CreatedDateUtc)
.选择(x=>x.FilterList)
.Take(批量大小)
.ProjectTo()
.ToListAsync();
}
我想你需要这样的东西:
var res = await dbContext
.FilterLists
.OrderBy(q => q.Snapshots.Any())
.ThenBy(q => q.Snapshots
.Select(w => w.CreatedDateUtc)
.OrderByDescending(e => e).FirstOrDefault())
.Take(batchSize)
.ProjectTo<FilterListViewUrlDto>()
.ToListAsync();
var res=await dbContext
.过滤者
.OrderBy(q=>q.Snapshots.Any())
.ThenBy(q=>q.Snapshots
.Select(w=>w.CreatedDateUtc)
.OrderByDescending(e=>e).FirstOrDefault()
.Take(批量大小)
.ProjectTo()
.ToListAsync();