HW2 GetFigureTypeFromName LINQ

public enum FigureType { Triangle, Square, Rectangle, Parallelogram, Rhombus, Trapezoid, Circle }

class Program
{
static Dictionary<string, FigureType> figureNameDic = new Dictionary<string, FigureType>();
static void SetFigureNameDictionary()
{
figureNameDic.Add(“삼각형”, FigureType.Triangle);
figureNameDic.Add(“Triangle”, FigureType.Triangle);
figureNameDic.Add(“TRIANGLE”, FigureType.Triangle);
figureNameDic.Add(“정사각형”, FigureType.Square);
figureNameDic.Add(“Square”, FigureType.Square);
figureNameDic.Add(“SQUARE”, FigureType.Square);
figureNameDic.Add(“직사각형”, FigureType.Rectangle);
figureNameDic.Add(“Rectangle”, FigureType.Rectangle);
figureNameDic.Add(“RECTANGLE”, FigureType.Rectangle);
figureNameDic.Add(“평행사변형”, FigureType.Parallelogram);
figureNameDic.Add(“Parallelogram”, FigureType.Parallelogram);
figureNameDic.Add(“PARALLELOGRAM”, FigureType.Parallelogram);
figureNameDic.Add(“마름모꼴”, FigureType.Rhombus);
figureNameDic.Add(“Rhombus”, FigureType.Rhombus);
figureNameDic.Add(“RHOMBUS”, FigureType.Rhombus);
figureNameDic.Add(“사다리꼴”, FigureType.Trapezoid);
figureNameDic.Add(“Trapezoid”, FigureType.Trapezoid);
figureNameDic.Add(“TRAPEZOID”, FigureType.Trapezoid);
figureNameDic.Add(“원”, FigureType.Circle);
figureNameDic.Add(“Circle”, FigureType.Circle);
figureNameDic.Add(“CIRCLE”, FigureType.Circle);
}

static FigureType? GetFigureTypeFromName(string str)
{
return figureNameDic.Where(x => x.Key.Contains(str)).Select(x => (FigureType?)x.Value).FirstOrDefault(); // FirstOrDefault returns the default value of a type if no item matches the predicate.
}

// 도형(Figure) 입력
static FigureType? GetInputFigure()
{
FigureType? inputFigure = null;
do
{
Console.Write(“도형의 종류를 입력해주세요(삼각형/정사각형/직사각형/평행사변형/마름모꼴/사다리꼴/원 등등 또는 Triangle/Square/Rectangle): “);
string str = Console.ReadLine();
inputFigure = GetFigureTypeFromName(str); // 입력된 도형에 따라서 enum 도형(Figure) 얻기
Console.WriteLine(“사용자 입력:{0} enum 도형:{1}”, str, inputFigure);
} while (inputFigure == null);
return inputFigure;
}

static void Main(string[] args)
{
SetFigureNameDictionary();
do
{
FigureType? inputFigure = GetInputFigure();
Console.WriteLine(“FigureType={0}”, inputFigure);
Console.WriteLine(“프로그램을 종료하려면 ESCAPE 키를 누르세요. 다시 계산하려면 ENTER 키를 누르세요.”);
} while (Console.ReadKey().Key != ConsoleKey.Escape);
}
}

LINQ

    class Category
    {
        public string Name { get; set; }
        public int ID { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            // The Three Parts of a LINQ Query:
            //  1. Data source.
            int[] numbers = new int[7] { 0, 1, 2, 3, 4, 5, 6 };

            // 2. Query creation.
            // numQuery is an IEnumerable<int>
            var numQuery =
                from num in numbers
                where (num % 2) == 0
                select num;

            // 3. Query execution.
            foreach (int num in numQuery)
            {
                Console.Write(“{0,1} “, num);
            }
            Console.WriteLine();

            // LIST
            List<Person> pList = new List<Person>();
            pList.Add(new Person(“둘리”, 1, “123-4568-9012”, “대한민국”));
            pList.Add(new Person(“희동이”, 3, “000-0000-0000”, “서울”));
            pList.Add(new Person(“고길동”, 2, “000-0000-0000”, “서울”));
            pList.Add(new Person(“도우너”, 5, “000-0000-0000”, “온따빠야별”));
            pList.Add(new Person(“또치”, 4, “000-0000-0000”, “미국”));
            pList.Insert(2, new Person(“마이콜”, 6, “000-0000-0000”, “서울”));

            // query syntax

            IEnumerable<Person> seoulQuery =
                from per in pList
                where per.Address == “서울”
                select per;

            foreach (var per in seoulQuery)
            {
                Console.WriteLine(per.Name + “, ” + per.ID);
            }
            Console.WriteLine();

            // query syntax
            IEnumerable<Person> personQuery =
                from p in pList
                where (p.ID < 2) || (p.Address == “서울”)
                orderby p.Name ascending
                select p;

            foreach (var p in personQuery)
            {
                Console.WriteLine(p.Name + “, ” + p.ID);
            }
            Console.WriteLine();

            // method syntax
            IEnumerable<Person> personQuery2 = pList.Where(p => (p.ID < 2) || (p.Address == “서울”)).OrderBy(p => p);
            foreach (var p in personQuery2)
            {
                Console.WriteLine(p.Name + “, ” + p.ID);
            }
            Console.WriteLine();

            // JOIN
            List<Category> categories = new List<Category>()
            {
                new Category() { Name=”서울”, ID=1 },
                new Category() { Name=”대한민국”, ID=2 },
                new Category() { Name=”미국”, ID=3 },
                new Category() { Name=”온따빠야별”, ID=4 },
            };

            var query = from p in pList
                        join cat in categories on p.Address equals cat.Name
                        select new
                        {
                            p.Name,
                            p.Address,
                            cat.ID
                        };
            Console.WriteLine(“Join Query”);
            foreach (var q in query)
            {
                Console.WriteLine(q.Name + “, ” + q.Address + “, ” + q.ID);
            }
            Console.WriteLine();
        }
    }

 

 

// 결과

0 2 4 6
희동이, 3
마이콜, 6
고길동, 2

고길동, 2
둘리, 1
마이콜, 6
희동이, 3

고길동, 2
둘리, 1
마이콜, 6
희동이, 3

Join Query
둘리, 대한민국, 2
희동이, 서울, 1
마이콜, 서울, 1
고길동, 서울, 1
도우너, 온따빠야별, 4
또치, 미국, 3