0

在 Linqpad 中,我可以看到正确的列表。但在代码中,放入列表集合后,排序依据不适用于 BeginDate。如果我将 BeginDate 与 Max 一起使用,它可以工作。我不明白我错在哪里?

            var templist =  contentRepository
                       .Get(q => (q.Status == (int)StatusEnum.Active) &&     
                           (q.CategoryId == category.GetHashCode() || q.Category.ParentId == category.GetHashCode())                   
                           && q.MinorVersion == 0
                           && q.MajorVersion > 0) 
                       .GroupBy(q => q.VersionId)
                       .OrderByDescending(q => q.Key)
                       .Select(q => new 
                       {
                           VersionId        = q.Key,
                           Id               = q.Max(x => x.Id),
                           MajorVersion     = q.Max(x => x.MajorVersion), 
                           UpdatedAt        = q.Max(x => x.UpdatedAt), 
                           //BeginDate        = q.Max(x=>x.BeginDate),
                           BeginDate        = (q.OrderByDescending(x => x.Id).Take(1).Select(x=>x.BeginDate)).First(),
                           Title = (q.OrderByDescending(x => x.Id).Take(1).Select(x => x.Title)).First(),
                           ShowOnHomePage   = (q.OrderByDescending(x => x.Id).Take(1).Select(x=>x.ShowOnHomePage)).First()
                        })
                       .OrderByDescending(x => x.BeginDate)                           
                       .Take(maxItemCount)
                       .ToList();

            List<ContentEntity> contents = new List<ContentEntity>();

        templist.ForEach(q => contents.Add(
                                         contentRepository
                                        .Get(x => x.VersionId == q.VersionId && x.MajorVersion == q.MajorVersion && x.MinorVersion == 0)
                                        .FirstOrDefault()                                        
                                        ));


        return contents.Where(q => q.ShowOnHomePage == true)
                       .OrderByDescending(q => q.MajorVersion)
                       .OrderByDescending(q => q.BeginDate)
                       .Take(maxItemCount)
                       .ToList();
4

1 回答 1

2

您是按 订购Id,而不是按订购BeginDate。等效代码

q.Max(x => x.BeginDate)

将会

q.OrderByDescending(x => x.BeginDate).Take(1).Select(x => x.BeginDate).First()

或简化

q.OrderByDescending(x => x.BeginDate).First().BeginDate
于 2013-03-11T13:05:13.517 回答