locked
Count program in table by month in Xamarin RRS feed

  • Question

  • User398771 posted

    ``` public class MyDateProgam { [PrimaryKey, AutoIncrement] public int Id { get; set; } public DateTime MyDate { get; set; } public string Program { get; set; } }

    List mydate = new List() { new MyDateProgam {Id="0", MyDate=01/01/2021}, Program="A" }, new MyDateProgam {Id="1", MyDate=01/14/2021}, Program="B" }, new MyDateProgam {Id="2", MyDate=01/21/2021}, Program="A" }, new MyDateProgam {Id="3", MyDate=02/04/2021}, Program="C" }, new MyDateProgam {Id="4", MyDate=02/14/2021}, Program="A" }, new MyDateProgam {Id="5", MyDate=02/27/2021}, Program="B" }, new MyDateProgam {Id="6", MyDate=03/07/2021}, Program="C" }, new MyDateProgam {Id="7", MyDate=03/11/2021}, Program="C" }, new MyDateProgam {Id="8", MyDate=03/18/2021}, Program="A" }, new MyDateProgam {Id="9", MyDate=03/27/2021}, Program="B" }, } ```

    Question: how to count of Program based on months in MyDate ?, so that the results are exactly like the table below:

    ``` | Jan | Feb | Mar


    A | 2 | 1 | 1 B | 1 | 1 | 1 C | 0 | 1 | 2 ```

    Wednesday, March 3, 2021 6:19 AM

Answers

  • User397775 posted

    @ycusoy said: how to display it on the console?

    foreach(var item in ProgramTable) { System.Diagnostics.Debug.WriteLine($"Month {item.Month}"); foreach(var subitem in item.Programs) { System.Diagnostics.Debug.WriteLine($"Program {subitem.ProgramName} Count {subitem.ProgramCount}"); } }

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, March 4, 2021 2:57 AM

All replies

  • User397775 posted

    var ProgramTable = mydate.GroupBy(m => m.MyDate.Month) .Select(p => new { Month = p.Key, ProgramsInMonth = p.Count(), Programs = p.GroupBy(n => n.Program) .Select(s => new { ProgramName = s.Key, ProgramCount = s.Count(), Programs = s.Select(x => x) }) });

    Wednesday, March 3, 2021 8:47 AM
  • User382871 posted

    how to count of Program based on months in MyDate Try using the method of the Linq library to group the data like below: List<MyDateProgam> progams = new List<MyDateProgam>() { ... }; var newList = progams.GroupBy(c => new { c.Program, c.MyDate.Month }).Select(data => new { Month = data.Key.Month, Program = data.Key.Program });

    Similar issue: https://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-columns


    Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.

    Wednesday, March 3, 2021 9:58 AM
  • User398771 posted

    how to display it on the console?

    Wednesday, March 3, 2021 3:30 PM
  • User398771 posted

    @Penni said: var ProgramTable = mydate.GroupBy(m => m.MyDate.Month) .Select(p => new { Month = p.Key, ProgramsInMonth = p.Count(), Programs = p.GroupBy(n => n.Program) .Select(s => new { ProgramName = s.Key, ProgramCount = s.Count(), Programs = s.Select(x => x) }) });

    I tried this example but the results for Program are not there. just : 1, 3 2, 3 3, 4

    Wednesday, March 3, 2021 9:12 PM
  • User398771 posted

    @Yelinzh said:

    how to count of Program based on months in MyDate Try using the method of the Linq library to group the data like below: List<MyDateProgam> progams = new List<MyDateProgam>() { ... }; var newList = progams.GroupBy(c => new { c.Program, c.MyDate.Month }).Select(data => new { Month = data.Key.Month, Program = data.Key.Program });

    Similar issue: https://stackoverflow.com/questions/5231845/c-sharp-linq-group-by-on-multiple-columns


    Xamarin forums are migrating to a new home on Microsoft Q&A! We invite you to post new questions in the Xamarin forums’ new home on Microsoft Q&A! For more information, please refer to this sticky post.

    When I add a new list on same MyDate as a random Program, Programs is not counted.

    Wednesday, March 3, 2021 9:15 PM
  • User397775 posted

    @ycusoy said: how to display it on the console?

    foreach(var item in ProgramTable) { System.Diagnostics.Debug.WriteLine($"Month {item.Month}"); foreach(var subitem in item.Programs) { System.Diagnostics.Debug.WriteLine($"Program {subitem.ProgramName} Count {subitem.ProgramCount}"); } }

    • Marked as answer by Anonymous Thursday, June 3, 2021 12:00 AM
    Thursday, March 4, 2021 2:57 AM
  • User398771 posted

    @Penni said: foreach(var item in ProgramTable) { System.Diagnostics.Debug.WriteLine($"Month {item.Month}"); foreach(var subitem in item.Programs) { System.Diagnostics.Debug.WriteLine($"Program {subitem.ProgramName} Count {subitem.ProgramCount}"); } }

    thanks @Penni , you save my day. cheers!

    Thursday, March 4, 2021 8:50 AM