将IGrouping转换为IQueryable

gri*_*egs 5 c# linq

我有一个名为CelebrityLocation的模型,除了其他属性之外,还有一个名人模特.

所以;

public partial class CelebrityLocation
  public Celebrity Celebrity {get; set;}
Run Code Online (Sandbox Code Playgroud)

我想得到一个所有CelebrityLocation对象的列表,但是Celebrity在其中分组CelebrityLocation.

我得到IGroupng的返回类型,但我需要将其转换为IQueryable<CelebrityLocation>.

以下是我到目前为止所尝试的内容.

IQueryable<CelebrityLocation> returnSet = (IQueryable<CelebrityLocation>) dc.CelebrityLocations.OrderByDescending(x => x.DateTime).GroupBy(x => x.Celebrity);
Run Code Online (Sandbox Code Playgroud)

编辑

在这种情况下,AutoMapper是否可行?

Kri*_*izz 5

你是否只想让IQueryable<CelebrityLocation>那些分组的元素彼此相邻?

如果是这样,这应该有所帮助:

IQueryable<CelebrityLocation> returnSet = dc.CelebrityLocations.OrderByDescending(x => x.DateTime).GroupBy(x => x.Celebrity).SelectMany(x => x);
Run Code Online (Sandbox Code Playgroud)


fuz*_*uzz 4

将您的查询更改为:

dc.CelebrityLocations.OrderByDescending(x => x.DateTime).AsQueryable().GroupBy(x => x.Celebrity).Select(x => x.First());
Run Code Online (Sandbox Code Playgroud)