Thursday 12 May 2016

C# - LINQ to List - Group and Count


Watch this example on YouTube

    public class test
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

    public partial class LINQ_List_Group_Count_test : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            List<test> myList = new List<test>(){
                new test(){Name ="Frank", Age =80},
                new test(){Name = "Bob", Age = 30},
                new test(){Name = "Bob", Age = 50},
                new test(){Name = "John", Age = 40}
            };
            var AllNames = from m in myList select m.Name;
            var UniqueNames = from a in myList
                              group a by a.Name into g
                              select g.Count();
            Response.Write(UniqueNames.Count().ToString());
            Response.Write("<br />");
            Response.Write(AllNames.Count().ToString());
           
        }

No comments:

Post a Comment