๐Ÿ™‡โ€โ™€๏ธ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);
}

ํƒœ๊ทธ:

์นดํ…Œ๊ณ ๋ฆฌ:

์—…๋ฐ์ดํŠธ: