Combination of Linq groups and linq groups

Source: Internet
Author: User

Combination of Linq groups and linq groups

There is a List, which requires grouping by date and combining the data of the same day with the implementation of Linq.

namespace ConsoleApplication1{    class Program    {        static void Main(string[] args)        {            List<Student> students = new List<Student>()            {                new Student(){ ID = 1 , Name ="guwei1", BirthDay=DateTime.Now.AddHours(-11), Phone="123456781"},                new Student(){ ID = 2 , Name ="guwei2", BirthDay=DateTime.Now.AddHours(2).AddDays(2), Phone="123456782"},                new Student(){ ID = 3 , Name ="guwei3", BirthDay=DateTime.Now.AddHours(-13), Phone="123456783"},                new Student(){ ID = 4 , Name ="guwei4", BirthDay=DateTime.Now.AddHours(4).AddDays(4), Phone="123456784"},            };            var result = from p in students                         group p by p.BirthDay.ToString("yyyy-MM-dd") into g                         let q = students.Where(x => x.BirthDay.ToString("yyyy-MM-dd") == g.Key)                         select new                         {                             ID = q.Select(y => y.ID),                             Name = q.Select(y => y.Name),                             BirthDay = g.Key,                             Phone = q.Select(y => y.Phone),                         };            foreach (var item in result)            {                Console.WriteLine("ID:{0} Name:{1} BirthDay:{2} Phone:{3}",                    string.Join(",", item.ID),                    string.Join(",", item.Name),                    item.BirthDay,                    string.Join(",", item.Phone));            }        }    }    public class Student    {        public int ID { get; set; }        public string Name { get; set; }        public DateTime BirthDay { get; set; }        public string Phone { get; set; }    }}


We can see that the data items are grouped by date, and the data items of other attributes are merged and displayed.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.