質問

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