简体   繁体   English

将IGrouping转换为IQueryable

[英]Convert IGrouping to IQueryable

I have a model called CelebrityLocation which has, amongst other properties, a Celebrity model. 我有一个名为CelebrityLocation的模型,除了其他属性之外,还有一个名人模特。

So; 所以;

public partial class CelebrityLocation
  public Celebrity Celebrity {get; set;}

I want to get a list of all the CelebrityLocation objects but group that by Celebrity within the CelebrityLocation . 我想获得所有CelebrityLocation对象的列表,但是Celebrity中的CelebrityLocation分组。

I get a return type of IGroupng but I need to convert that to an IQueryable<CelebrityLocation> . 我得到IGroupng的返回类型,但我需要将其转换为IQueryable<CelebrityLocation>

The below is what I have been trying so far. 以下是我到目前为止所尝试的内容。

IQueryable<CelebrityLocation> returnSet = (IQueryable<CelebrityLocation>) dc.CelebrityLocations.OrderByDescending(x => x.DateTime).GroupBy(x => x.Celebrity);

Edit 编辑

Is AutoMapper a viable solution in this case? 在这种情况下,AutoMapper是否可行?

Do you want to just get flat IQueryable<CelebrityLocation> just that grouped elements are next to each other? 你想只是让那些分组的元素彼此相邻吗? IQueryable<CelebrityLocation>

If so, this should help: 如果是这样,这应该有所帮助:

IQueryable<CelebrityLocation> returnSet = dc.CelebrityLocations.OrderByDescending(x => x.DateTime).GroupBy(x => x.Celebrity).SelectMany(x => x);

将您的查询更改为:

dc.CelebrityLocations.OrderByDescending(x => x.DateTime).AsQueryable().GroupBy(x => x.Celebrity).Select(x => x.First());

你可以调用AsQueryable()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM