Forum General

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Count program in table by month in Xamarin

ycusoyycusoy Member ✭✭
edited March 3 in General
public class MyDateProgam
    {
        [PrimaryKey, AutoIncrement]
        public int Id { get; set; }
        public DateTime MyDate { get; set; }
        public string Program { get; set; }
    }

List<MyDateProgam> mydate = new List<MyDateProgam>()
{
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
Tagged:

Best Answer

  • PenniPenni Member ✭✭
    Accepted Answer

    @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}");
                    }
                }
    

Answers

  • PenniPenni Member ✭✭
    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)
                        })
                    });
    
  • YelinzhYelinzh Member, Xamarin Team Xamurai
    edited March 3

    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.

  • ycusoyycusoy Member ✭✭

    how to display it on the console?

  • ycusoyycusoy Member ✭✭
    edited March 3

    @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

  • ycusoyycusoy Member ✭✭
    edited March 3

    @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.

  • PenniPenni Member ✭✭
    Accepted Answer

    @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}");
                    }
                }
    
  • ycusoyycusoy Member ✭✭
    > @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!
Sign In or Register to comment.