LINQ
Case 1
using System;
using System.Linq;
namespace LinqSample
{
class Program
{
static void Main(string[] args)
{
var sample = "I enjoy writing uber-software in C#";
var result = from c in sample
select c;
foreach (var item in result)
{
Console.Write(item);
}
Console.WriteLine();
Console.ReadKey();
}
}
}
Case 2 - where, orderby
using System;
using System.Linq;
namespace LinqSample
{
class Program
{
static void Main(string[] args)
{
var sample = "I enjoy writing uber-software in C#";
var result = from c in sample.ToLower()
where c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
orderby c ascending
select c;
foreach (var item in result)
{
Console.Write(item);
}
Console.WriteLine();
Console.ReadKey();
}
}
}
Case 3 - group
using System;
using System.Linq;
namespace LinqSample
{
class Program
{
static void Main(string[] args)
{
var sample = "I enjoy writing uber-software in C#";
var result = from c in sample.ToLower()
where c == 'a' || c == 'e' || c == 'i' || c == 'o' || c == 'u'
orderby c
group c by c;
foreach (var item in result)
{
Console.WriteLine($"{item.Key} - {item.Count()}");
}
Console.ReadKey();
}
}
}
Case 4 - Class
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqSample
{
class Program
{
static void Main(string[] args)
{
var people = new List<Person>
{
new Person{FirstName = "John", LastName = "Doe", Age = 25},
new Person{FirstName = "Jane", LastName = "Doe", Age = 26},
new Person{FirstName = "John", LastName = "Williams", Age = 40},
new Person{FirstName = "Samantha", LastName = "Williams", Age = 34},
new Person{FirstName = "Bob", LastName = "Walters", Age = 12},
};
var result = from p in people
where p.Age < 30 && p.LastName == "Doe"
orderby p.LastName
select p;
foreach (var item in result)
{
Console.WriteLine($"{item.LastName}, {item.FirstName} : {item.Age}");
}
Console.ReadKey();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
Case 5 - Class, group
using System;
using System.Collections.Generic;
using System.Linq;
namespace LinqSample
{
class Program
{
static void Main(string[] args)
{
var people = new List<Person>
{
new Person{FirstName = "John", LastName = "Doe", Age = 25},
new Person{FirstName = "Jane", LastName = "Doe", Age = 26},
new Person{FirstName = "John", LastName = "Williams", Age = 40},
new Person{FirstName = "Samantha", LastName = "Williams", Age = 34},
new Person{FirstName = "Bob", LastName = "Walters", Age = 12},
};
var result = from p in people
orderby p.LastName descending
group p by p.LastName;
foreach (var item in result)
{
Console.WriteLine($"{item.Key}({item.Count()})");
foreach (var p in item)
{
Console.WriteLine($" {p.LastName}, {p.FirstName} : {p.Age}");
}
}
Console.ReadKey();
}
}
public class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
https://www.youtube.com/watch?v=Z6YWWRUcJis
'C# > C# Common' 카테고리의 다른 글
?. ??= (0) | 2022.09.04 |
---|---|
Extension Methods (0) | 2022.09.03 |
Event (0) | 2022.08.30 |
Anonymous Methods and Lambda Expression (0) | 2022.08.30 |
Generic (0) | 2022.08.16 |