반응형
250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 | 31 |
Tags
- 콜백함수
- 인터넷프로토콜
- .NET
- EntityFramework
- 자바스크립트
- 객체리터럴
- 자바스크립트틱택토
- Blazor
- 자바스크립트객체리터럴
- 틱택토구현
- 인프런강좌
- 자바스크립트함수
- c#
- 자바스크립트파라미터
- sort
- 이벤트리스너
- 인프런
- 인프런강의
- 인프런자바스크립트
- 코딩
- 인프런인강
- 제로초
- slice
- 인프런무료강좌
- 비주얼스튜디오
- 고차함수
- HTTP
- NPM
- 자바스크립트recude
- 객체의비교
Archives
- Today
- Total
샐님은 개발중
1. GROUP BY (단일,여러개) 본문
728x90
반응형
using System;
using System.Linq;
class Program
{
static void Main(string[] args)
{
List<Customer> list = new List<Customer>();
list.Add(new Customer() { Id = 1, Name = "홍길동", Country = "한국", Age = 22 });
list.Add(new Customer() { Id = 2, Name="이순신", Country="한국", Age = 21});
list.Add(new Customer() { Id = 3, Name="김갑동", Country="한국", Age = 22});
list.Add(new Customer() { Id = 4, Name="차우림", Country="미국", Age = 24});
list.Add(new Customer() { Id = 5, Name="고길동", Country="중국", Age = 22});
list.Add(new Customer() { Id = 5, Name="제시", Country="미국", Age = 22});
list.Add(new Customer() { Id = 5, Name="미카", Country="일본", Age = 21});
list.Add(new Customer() { Id = 5, Name="미사", Country="일본", Age = 21});
// 나라별 group by
var result =
from c in list
group c by c.Country;
var result2 =
list.GroupBy(c => c.Country);
foreach(IGrouping<string, Customer> group in result)
{
Console.WriteLine(group.Key + ":");
foreach (Customer c in group)
Console.WriteLine(" " + c.Name);
}
Console.WriteLine("group by 여러개");
//Country 와 Age 로 group by 및 Count(), Sum(), Average() 를 사용해서 집계할 수 있다.
var result3 =
list.GroupBy(x => new
{
x.Country,
x.Age
})
.Select(x => new CustomerGroup
{
Country = x.Key.Country,
Age = x.Key.Age,
Sum = x.Count()
});
foreach(var i in result3 ) {
Console.WriteLine(i.Country + i.Age + " " + i.Sum +" 명 입니다.");
}
}
public class Customer
{
public int Id { get; set; }
public string? Name { get; set; }
public int Age { get; set; }
public string? Country { get; set; }
public string? NicName { get; set; }
}
public class CustomerGroup
{
public int Sum { get; set; }
public string? Country { get; set; }
public int Age { get; set; }
}
}
728x90
반응형
'LINQ' 카테고리의 다른 글
PIVOT :Transform data from rows to column (0) | 2024.04.08 |
---|---|
LINQ 에서 String 문자열을 DateTime 형식과 비교 (0) | 2024.04.02 |
2. Join (0) | 2024.02.20 |