【发布时间】:2021-04-28 11:05:06
【问题描述】:
我有一个很长的 Linq 查询,我正试图在该查询的任何索引中获取一个数据。
我的查询是:
public IEnumerable<WebFairField> WebFairFieldForFair(Guid ID,int index)
{
return TradeTurkDBContext.WebFairField.Where(x => x.DataGuidID==ID)
.Include(x => x.Category)
.ThenInclude(x=>x.MainCategory).AsSplitQuery()
//
.Include(x=>x.FairSponsors)
.ThenInclude(x=>x.Company)
.ThenInclude(x=>x.FileRepos).AsSplitQuery()
//
.Include(x=>x.WebFairHalls.Take(1).ElementAt(index)) //Thats the point where i stuck*
.ThenInclude(x=>x.HallSeatingOrders)
.ThenInclude(x=>x.Company)
.ThenInclude(x=>x.FileRepos).AsSplitQuery()
//
.Include(x=>x.HallExpertComments).AsSplitQuery()
.Include(x=>x.Products).AsSplitQuery()
.Include(x=>x.FairSponsors).AsSplitQuery()
.AsNoTrackingWithIdentityResolution()
.ToList();
}
当我这样做时,它会给我一个错误:Collection navigation access can be filtered by composing Where, OrderBy,ThenBy,Skip or Take operations.
我知道我必须对这些数据进行排序,但我不知道该怎么做。谁能告诉我应该如何对该查询的数据进行排序?
感谢您的任何建议!
【问题讨论】:
-
类似
Skip(index - 1).Take(1)的东西?您也可能需要应用一些排序来获得一致的结果。 -
在
Take(1)之后还有ElementAt在我能想象的任何情况下都没有任何意义。 -
感谢您的建议大师它有效,但我想问一些事情。我为我的分页做那个动作,但我想知道是否有比我做的更好的方法。有更好的方法吗?
-
对我来说,这需要对您的项目领域有更多的了解。可能 - 是的,但这取决于。
-
感谢您的努力!
标签: linq sorting asp.net-core entity-framework-core ef-core-5.0