我有一個 groupby 函式,它在選擇中創建一系列列。對于一個列,我想回傳保存在該列標題下的欄位的結果陣列,但我似乎無法弄清楚如何在 linq 中撰寫此查詢。
示例代碼
var res = students.GroupBy(x => x.ClassId).Select(a => new {
ClassHours = a.Sum(r => r.Hours),
AvgAttendance = a.Average(r => r.AttendanceRate),
NumStudents = a.Count(),
// I want to get a list of all student names but not sure how to do this,
I want a array return of student names, I tried with for each but this doesn't seem to work but I'm not sure what to do
StudentNames = a.ForEach(r => return r.StudentName)
// I want to do something like this but make the rest a array e.g. StudentNames = ["Blaise", "Sasha", "Jeff"]
});
uj5u.com熱心網友回復:
如果不知道你的物件是什么樣子就很難確定,但大概你可以使用Select?
StudentNames = a.Select(r => r.StudentName).ToArray()
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/442863.html
上一篇:使用DTO類時可以為空的參考型別
