EF Core:Distinct和OrderBy奇怪的行为

时间:2016-07-17 18:47:49

标签: c# entity-framework

我将旧项目迁移到新EF Core并发现此问题

我的旧代码:

private IQueryable<SeriesData> GetSeriesData(IQueryable<Series> query, long? userId = null)
{
    DateTime date = DateTime.Today.AddMonths(1);

    IQueryable<SeriesData> seriesDataQuery = query.Select(x => new SeriesData
    {
        Series = x,
        Subscribed = userId.HasValue && x.Subscriptions.Any(y => y.UserId == userId),
        CurrentSeasonNumber = x.Episodes.Where(z => z.ReleaseDate.HasValue && z.ReleaseDate < date).Max(y => y.SeasonNumber),
        Channel = x.Channel,
        Country = x.Channel.Country,
        ReleaseGroups =
            x.Episodes.SelectMany(z => z.Releases)
                .Select(y => y.ReleaseGroup)
                .Distinct()             // 1
                .OrderBy(y => y.Name)   // 2
                .Select(r => new ReleaseGroupData
                {
                    ReleaseGroup = r,
                    Subscribed =
                        userId.HasValue &&
                        x.Subscriptions.Any(y => y.UserId == userId && y.ReleaseGroupId == r.Id)
                }).ToList()
    });

    return seriesDataQuery;
}

当我执行此查询时,我得到&#34; InvalidOperationException:Sequence包含多个元素&#34;例外 但是,如果我换掉第1行和第2行,一切都会有效。

0 个答案:

没有答案