none
Linq groupby EF RRS feed

  • Question

  • Hi. I have a problem
    I have a table with this values
    sex - zipcode - city - region - age
    and the user can select one or more parameter to have a report. Example

    Selection SEX
    male 100
    female 50

    Selection SEX ZIPCODE
    male 20127 50
    male 20128 50
    female 21213 30
    female 21578 20

    I have to generate this table in linq. I use EF, but it is not a problem to read all record and generate after ToList() the table
    someone can help me? thanks

    matteo ghetti

    Tuesday, November 15, 2016 12:15 PM

All replies

  • Hi kuria,

    You could use if condition and return different result. like this:

     
    if(condition 1)
    {
        var query = db.Persons.GroupBy(x => new { x.sex, x.region}).Select(o=> new { sex = o.Key.sex, reg= o.Key.region,count=o.Count()});
    }
    if(condition 2 )
    {
       var query = db.Persons.GroupBy(x => x.sex);
    
    }
    // other condition.

    In addition, you could also use dynamic group by, for more information, please refer to:

    http://stackoverflow.com/questions/3929041/dynamic-linq-groupby-multiple-columns

    Best regards,

    Cole Wu


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, November 16, 2016 9:30 AM
    Moderator
  • I have 5 checkbox condition, and these can be compinated in every way. So in this case i will have 5! options (5*4*3*2*1). too much if...


    matteo ghetti

    Wednesday, November 16, 2016 9:51 AM