문제

i have this linq query:

    var query = from mpItem in MPay
                where mpItem.EndDate > System.DateTime.Now.Date
                group mpItem by mpItem.IdGroup into mpItemGrouped
                let minEndDate = mpItemGrouped.Min(p => p.EndDate)
                select new
                {
                    Id = mpItemGrouped.Key,
                    EndDate = mpItemGrouped.Min(p => p.EndDate),
                    Name = mpItemGrouped.Min(p => p.IdGroupModel.GroupName),
                    Price = mpItemGrouped.Min(p => p.PaySumIndividual)
                };

this query should select for each IdGroup the row with the minimum EndDate this query is selecting the minimum EndDate with another row values.

도움이 되었습니까?

해결책

Order group by EndDate in ascending order and select first item from group - that will be item with min date. Then use this item in select statement

   var query = from MPayItem in MPay
                where mpItem.EndDate > System.DateTime.Now.Date
                group mpItem by mpItem.IdGroup into mpItemGrouped
                let minItem = mpItemGrouped.OrderBy(p => p.EndDate).First()
                select new
                {
                    Id = mpItemGrouped.Key,
                    EndDate = minItem.EndDate,
                    Name = minItem.IdGroupModel.GroupName,
                    Price = minItem.PaySumIndividual
                };
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top