기본적으로 SQL 이랑 비슷한것같다. 예제만 늘어둘테니 알아서 익히도록
using System;
using System.Linq;
namespace test
{
class Program
{
static void Main(string[] args)
{
String[] names = new String[]{ "qq", "www","ee", "rr", "aaa"};
var nameQuery = from name in names
where name.Length == 3
select name;
foreach(string name in nameQuery) // 결과 출력
Console.WriteLine(name);
}
}
}
결과는 aaa,www
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace test
{
class Program
{
static void Main(string[] args)
{
//Student 객체 선언
Student student1 = new Student();
Student student2 = new Student();
Student student3 = new Student();
//Student형의 List 변수 student_List선언
List<Student> student_List = new List<Student>();
student1.name = "범범조조";
student1.age = 26;
student1.grade = 4;
student2.name = "홍길동";
student2.age = 24;
student2.grade = 3;
student3.name = "장영실";
student3.age = 39;
student3.grade = 2;
student_List.Add(student1);
student_List.Add(student2);
student_List.Add(student3);
//20대 학생들을 조회하는 LINQ 구문
var Twenty_Student = from _student in student_List
where _student.age >= 20 && _student.age < 30
orderby _student.age ascending
select _student;
//30대 학생들을 조회하는 LINQ 구문
var Thirty_Student = from _student in student_List
where _student.age > 30
orderby _student.age ascending
select _student;
foreach(var Stu in Twenty_Student)
{
Console.WriteLine("나이가 20대인 학생은 = {0} 입니다.", Stu.name);
}
var queryStudent = from student in listAverage
join hobby in listHobby on student.Name equals hobby.Name
select new { Name = student.Name, Average = student.Average, Hobby = hobby.Hobby };
foreach (var studentGroup in queryStudent)
Console.WriteLine("이름: {0}\n\t평균: {1}점\n\t취미: {2}", studentGroup.Name, studentGroup.Average, studentGroup.Hobby);