Showing posts with label groupby multiple value. Show all posts
Showing posts with label groupby multiple value. Show all posts

Monday 20 January 2020

groupby multiple colunm

Step1: Suppose you have classes:

 public class Teacher
    {
        public string School { get; set; }
        public string Friend { get; set; }
        public string Book { get; set; }
        public List<Student> Children { get; set; }
    }

    public class Student
    {
        public string School { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Friend { get; set; }
        public string Mother { get; set; }
        public string Book { get; set; }
    }

Step2: In step1 you have created class, now on your controller action method you can set some mock data as example given below:  List<Student> student = GetList();

Step3: after that we have apply query group by:-
 public class TestController : ControllerBase
    {

        [HttpGet]
        [Route("groupby")]
        public async Task<IActionResult> TestGroupBy()
        {
            List<Student> student = GetList();
            var result =
            from c in student
            group c by new { c.School, c.Friend, c.Book, } into gcs
            select new Teacher()
            {
                School = gcs.Key.School,
                Friend = gcs.Key.Friend,
                Book = gcs.Key.Book,
                Children = gcs.ToList(),
            };
            return Ok(result);
        }

        private static List<Student> GetList()
        {
            return new List<Student>()
                {
                    new Student()
                        {School = "S1", Book = "Book1", Friend = "Hamid", Name = "Stev"},
                    new Student()
                        {School = "S2", Book = "Book1", Friend = "Hamid", Name = "Bill"},
                    new Student()
                        {School = "S3", Book = "Book1", Friend = "Smith", Name = "Root"},
                    new Student()
                        {School = "S2", Book = "Book1", Friend = "Finch", Name = "Root"},
                };
        }
    } Ok(result);
        }

Step4: Result:
[
  {
    "school": "S1",
    "friend": "Hamid",
    "book": "Book1",
    "children": [
      {
        "school": "S1",
        "name": "Stev",
        "address": null,
        "friend": "Hamid",
        "mother": null,
        "book": "Book1"
      },
      {
        "school": "S1",
        "name": "Stev",
        "address": null,
        "friend": "Hamid",
        "mother": null,
        "book": "Book1"
      }
    ]
  },
  {
    "school": "S2",
    "friend": "Smith",
    "book": "Book1",
    "children": [
      {
        "school": "S2",
        "name": "Root",
        "address": null,
        "friend": "Smith",
        "mother": null,
        "book": "Book1"
      },
      {
        "school": "S2",
        "name": "Root",
        "address": null,
        "friend": "Smith",
        "mother": null,
        "book": "Book1"
      }
    ]
  }
]