Search
 
SCRIPT & CODE EXAMPLE
 

CSHARP

groupby in linq

var results = from p in persons
              group p.car by p.PersonId into g
              select new { PersonId = g.Key, Cars = g.ToList() };
Comment

groupby in linq

        //Method Syntax

        List<Result> results2 = persons
            .GroupBy(p => p.PersonId, 
                     (k, c) => new Result()
                             {
                                 PersonId = k,
                                 Cars = c.Select(cs => cs.car).ToList()
                             }
                    ).ToList();

    
Comment

linq group by

var results = persons.GroupBy(
    p => p.PersonId, 
    p => p.car,
    (key, g) => new { PersonId = key, Cars = g.ToList() });
Comment

group-by-in-linq

using System;
using System.Linq;
using System.Collections.Generic;

class Person
{ 
    public int PersonId; 
    public string car  ; 
}

class Result
{ 
   public int PersonId;
   public List<string> Cars; 
}

public class Program
{
    public static void Main()
    {
        List<Person> persons = new List<Person>()
        {
            new Person { PersonId = 1, car = "Ferrari" },
            new Person { PersonId = 1, car = "BMW" },
            new Person { PersonId = 2, car = "Audi"}
        };

        //With Query Syntax

        List<Result> results1 = (
            from p in persons
            group p by p.PersonId into g
            select new Result()
                {
                    PersonId = g.Key, 
                    Cars = g.Select(c => c.car).ToList()
                }
            ).ToList();

        foreach (Result item in results1)
        {
            Console.WriteLine(item.PersonId);
            foreach(string car in item.Cars)
            {
                Console.WriteLine(car);
            }
        }

        Console.WriteLine("-----------");

        //Method Syntax

        List<Result> results2 = persons
            .GroupBy(p => p.PersonId, 
                     (k, c) => new Result()
                             {
                                 PersonId = k,
                                 Cars = c.Select(cs => cs.car).ToList()
                             }
                    ).ToList();

        foreach (Result item in results2)
        {
            Console.WriteLine(item.PersonId);
            foreach(string car in item.Cars)
            {
                Console.WriteLine(car);
            }
        }
    }
}
Comment

PREVIOUS NEXT
Code Example
Csharp :: entity framework with query C# 
Csharp :: how to populate list in c# 
Csharp :: How to use the protected keyword in C# 
Csharp :: c# null conditional 
Csharp :: c# clear console read chache 
Csharp :: how-to-add-new-column-with-value-to-the-existing-datatable 
Csharp :: check if two date ranges overlap c# 
Csharp :: aspx element visibility ould not find 
Csharp :: generate UUID id for my entities 
Csharp :: listview inter thread operation not valid 
Csharp :: nunit cleanup after all tests 
Csharp :: c# minimise form 
Csharp :: unity rotate around point 
Csharp :: multiply structs c# 
Csharp :: winform fixed size 
Csharp :: how to check url has parameter in c# 
Csharp :: c# create a dummy class 
Csharp :: get gameobject active state 
Csharp :: c# split multiple options 
Csharp :: disable alt + f4 in c# forms 
Csharp :: Create Text File and Write 
Csharp :: begininvoke async c# 
Csharp :: c# ip address to string 
Csharp :: asp.net listbox disable selection 
Csharp :: c# list contains null 
Csharp :: c# async and await example 
Csharp :: unity how to check serialized enum 
Csharp :: c# convert list to string and back 
Csharp :: how to set a color of text in unity 2020 script 
Csharp :: ontriggerenter unity not working 
ADD CONTENT
Topic
Content
Source link
Name
2+8 =