c# - Linq take 3 records from each category -


i have column "category" in table , want take 3 rows each category. here example of table:

 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 

and why want back:

 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 

how can done linq?

i tried this:

.groupby(x => x.category).take(3) 

but after cant use .select

update: when using selectmany i'm getting error:

query source not identified: itemname = x, itemtype = system.linq.igrouping`2[system.string,adde.models.notification], expression = igrouping`2 x in {from notification w in value(nhibernate.linq.nhqueryable`1[adde.models.notification]) ([w].userid == 04bccede-46d0-44f8-814b-346d5510acb8) orderby [w].time asc select [w] => groupby([w].category, [w])}

update 2: here code:

.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)                                                          }) 

you want use this:

var result = data.groupby(x => x.category)                  .selectmany(x => x.take(3)); 

Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -