013. LINQ Query/Method Syntax
반응형

이번엔 LINQ다..

 

Query Syntax는 Query문을 수행해주는 것이라 생각하면 속편하다.

(DB의 Query를 생각해주자)

 

바로 코드..

 

이건 xaml

1
2
3
4
5
6
7
8
9
10
11
12
13
<Window x:Class="TestProject.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:TestProject"
        mc:Ignorable="d"
        Title="MainWindow" Height="217.5" Width="332">
    <Grid>
        <TextBlock x:Name="tb"/>
    </Grid>
</Window>
 
cs

 

이건 Code-Behind

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace TestProject
{
    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
 
 
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            List<Student> students = new List<Student>();
            students.Add(new Student
            {
                ID = 1,
                Name = "Kapil"
            });
 
            students.Add(new Student
            {
                ID = 2,
                Name = "James"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Michael"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Emily"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Megan"
            });
 
            students.Add(new Student
            {
                ID = 1,
                Name = "Stephanie"
            });
 
            students.Add(new Student
            {
                ID = 2,
                Name = "Daniel"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Hunter"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Joshua"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Kyle"
            });
 
            var result = from s in students
                         where s.Name.Contains("J")
                         select s.Name;
 
            tb.Text = string.Empty;
 
            foreach (var item in result)
            {
                tb.Text += item + Environment.NewLine;
            }
        }
    }
}
 
cs

 

코드가 길어지면서.... 그냥 스샷찍던거를 Color Scripter 이용하기로.......

 

아무튼.... 별거없다... List에 학생정보를 가지고 있고...

 

거기서 Query문을 통해 뽑고자 하는 조건을 넣어준다.

 

나같은 경우에는 Where문에서 적힌거대로 이름에 J가 들어가는 학생을 찾아주었다.

 

 

간단하게 찾아진다...

 

그럼 이번엔 Method Syntax!

 

이건 함수마냥 넣는다는 의미로 Method이다.. 결국 Query와 동일한 기능!

 

xaml 코드는 위와 동일하고..

 

code behind만 다르다..

 

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
 
namespace TestProject
{
    class Student
    {
        public int ID { get; set; }
        public string Name { get; set; }
    }
 
 
    /// <summary>
    /// MainWindow.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
 
            List<Student> students = new List<Student>();
            students.Add(new Student
            {
                ID = 1,
                Name = "Kapil"
            });
 
            students.Add(new Student
            {
                ID = 2,
                Name = "James"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Michael"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Emily"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Megan"
            });
 
            students.Add(new Student
            {
                ID = 1,
                Name = "Stephanie"
            });
 
            students.Add(new Student
            {
                ID = 2,
                Name = "Daniel"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Hunter"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Joshua"
            });
 
            students.Add(new Student
            {
                ID = 3,
                Name = "Kyle"
            });
 
 
            var result = students.Where(s => s.Name.Contains("J")).Select(s => s.Name);
 
            tb.Text = string.Empty;
 
            foreach (var item in result)
            {
                tb.Text += item + Environment.NewLine;
            }
 
        }
    }
}
 
cs

 

선언한 list에서 where문을 통해 특정조건의 사람을 찾고 select한다.

중간에 s=> 이런식으로 들어간것은 내용을 학생을 s라는 변수로 선언하는것이다. s든 다른 문자든 상관없다.

 

수행시 결과는 Query Syntax와 동일하다

반응형