C# Rookiss Part6 ์น์๋ฒ : LINQ
๐โโ๏ธLINQ
var players =
from p in _players
where p.ClassType == ClassType.Knight && p.Level >= 50
orderby p.Level
select p;
๐ชLINQ 1
public enum ClassType
{
Knight,
Archer,
Mage
}
public class Player
{
public ClassType ClassType { get; set; }
public int Level { get; set; }
public int Att { get; set; }
public int Hp { get; set; }
}
class Program
{
static List<Player> _players = new List<Player>();
//LINQ
static void Main(string[] args)
{
Random rand = new Random();
for (int i = 0; i < 100; i++)
{
ClassType type = ClassType.Knight;
switch (rand.Next(0, 3))
{
case 0:
type = ClassType.Knight;
break;
case 1:
type = ClassType.Archer;
break;
case 2:
type = ClassType.Mage;
break;
}
Player player = new Player()
{
ClassType = type,
Level = rand.Next(1, 100),
Att = rand.Next(1,100),
Hp = rand.Next(1, 1000)
};
_players.Add(player);
}
// Q) ๋ ๋ฒจ์ด 50 ์ด์์ธ Knight๋ง ์ถ๋ ค๋ด์, ๋ ๋ฒจ์ ๋ฎ์ -> ๋์ ์์๋ก ์ ๋ ฌ
// ์ผ๋ฐ ๋ฒ์
{
List<Player> players = GetHightLevelKnight();
foreach (Player p in players)
{
Console.WriteLine($"{p.Level} {p.Hp}");
}
}
//LINQ๋ฒ์
{
// from (foreach)
// where (ํํฐ ์ญํ = ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ฐ์ดํฐ๋ง ๊ฑธ๋ฌ๋ธ๋ค)
// orderby (์ ๋ ฌ์ ์ํ. ๊ธฐ๋ณธ์ ์ผ๋ก๋ ์ค๋ฆ์ฐจ์ ascending / descending
// select (์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์ถ์ถ -> ๊ฐ๊ณตํด์ ์ถ์ถ?)
var players =
from p in _players
where p.ClassType == ClassType.Knight && p.Level >= 50
orderby p.Level
select p;
foreach (Player p in players)
{
Console.WriteLine($"{p.Level} {p.Hp}");
}
}
}
static public List<Player> GetHightLevelKnight()
{
List<Player> players = new List<Player>();
foreach (Player player in _players)
{
if (player.ClassType != ClassType.Knight)
continue;
if (player.Level < 50)
continue;
players.Add(player);
}
players.Sort((lhs, rhs) => { return lhs.Level - rhs.Level; });
return players;
}
}
๐ชLINQ 2
c# linq methods์์ ๊ณต๋ถ!
// Q) ๋ ๋ฒจ์ด 50 ์ด์์ธ Knight๋ง ์ถ๋ ค๋ด์, ๋ ๋ฒจ์ ๋ฎ์ -> ๋์ ์์๋ก ์ ๋ ฌ
// ์ผ๋ฐ ๋ฒ์
{
List<Player> players = GetHightLevelKnight();
foreach (Player p in players)
{
Console.WriteLine($"{p.Level} {p.Hp}");
}
}
//LINQ๋ฒ์
{
// from (foreach)
// where (ํํฐ ์ญํ = ์กฐ๊ฑด์ ๋ถํฉํ๋ ๋ฐ์ดํฐ๋ง ๊ฑธ๋ฌ๋ธ๋ค)
// orderby (์ ๋ ฌ์ ์ํ. ๊ธฐ๋ณธ์ ์ผ๋ก๋ ์ค๋ฆ์ฐจ์ ascending / descending
// select (์ต์ข
๊ฒฐ๊ณผ๋ฅผ ์ถ์ถ -> ๊ฐ๊ณตํด์ ์ถ์ถ?)
var players =
from p in _players
where p.ClassType == ClassType.Knight && p.Level >= 50
orderby p.Level ascending
select p;
foreach (Player p in players)
{
Console.WriteLine($"{p.Level} {p.Hp}");
}
}
// ์ค์ฒฉ from
// ex) ๋ชจ๋ ์์ดํ
๋ชฉ๋ก์ ์ถ์ถ (์์ดํ
id < 30)
{
var playerItems = from p in _players
from i in p.Items
where i < 30
select new { p, i };
var li = playerItems.ToList();
}
// grouping
{
var playersByLevel = from p in _players
group p by p.Level into g
orderby g.Key
select new { g.Key, Plaers = 0 };
}
// join (๋ด๋ถ ์กฐ์ธ)
// outer join (์ธ๋ถ ์กฐ์ธ)
{
List<int> levels = new List<int>() { 1, 5, 10 };
var playerLevels =
from p in _players
join l in levels
on p.Level equals l
select p;
}
// LINQ ํ์ค ์ฐ์ฐ์
{
var players =
from p in _players
where p.ClassType == ClassType.Knight && p.Level >= 50
orderby p.Level ascending
select p;
_players
.Where(p => p.ClassType == ClassType.Knight && p.Level >= 50)
.OrderBy(p => p.Level)
.Select(p => p);
}