1

テーブルに「カテゴリ」列があり、各カテゴリから 3 行を取得したいと考えています。ここに私のテーブルの例:

 ID | Category | Text
----------------------
 01 |    TST   | Text here
 02 |    TST   | Text2 here
 03 |    TST   | Text3 here
 04 |    TST   | Text4 here
 01 |   TST02  | Text here
 02 |   TST02  | Text2 here
 03 |   TST02  | Text3 here
 04 |   TST02  | Text4 here
 05 |   TST02  | Text5 here

そして、これが私が戻りたい理由です:

 ID | Category | Text
----------------------
 01 |    TST   | Text here
 02 |    TST   | Text2 here
 03 |    TST   | Text3 here
 01 |   TST02  | Text here
 02 |   TST02  | Text2 here
 03 |   TST02  | Text3 here

どうすればLINQでそれを行うことができますか?

私はこのようにしようとしました:

.GroupBy(x => x.Category).Take(3)

しかし、その後は使用できません.Select

更新: SelectMany を使用するとエラーが発生します:

クエリ ソースを特定できませんでした: ItemName = x、ItemType = System.Linq.IGrouping`2[System.String,ADDE.Models.Notification]、Expression = from IGrouping`2 x in {from Notification w in value(NHibernate.Linq) .NhQueryable`1[ADDE.Models.Notification]) where ([w].UserId == 04bccede-46d0-44f8-814b-346d5510acb8) orderby [w].Time asc select [w] => GroupBy([w].カテゴリ、[w])}

更新 2: これが私のコードです:

.GroupBy(x => x.Category).SelectMany(x => x.Take(3)).Select(
                                                     s =>
                                                     new NotificationData
                                                         {
                                                             Category = s.Category,
                                                             Text = s.Text,
                                                             Time = DateTime.Now.Subtract(s.Time)
                                                         })
4

1 に答える 1

10

これを使用したい:

var result = data.GroupBy(x => x.Category)
                 .SelectMany(x => x.Take(3));
于 2013-04-05T13:09:49.880 に答える