[code csharp]using System;
using System.Collections.Generic;
using System.Linq;
public class QueryTest
{
public static void Start()
{
int[] scores = { 90, 71, 82, 93, 75, 85 };
IEnumerable<int> scoreQuery =
from score in scores
where score > 85
orderby score descending
select score;
foreach (int Result in scoreQuery)
{
Console.WriteLine(Result);
}
string[] PhoneNum = { "010-4617-0000", "010-5555-0000", "011-8728-0000" };
IEnumerable<string> queryFirstNum =
from ph in PhoneNum
let editedNum = ph.Replace("0000", "0167")
select editedNum;
foreach (string str in queryFirstNum)
{
Console.WriteLine(str);
}
}
}[/code]데이터베이스에서 쓰이는 그 쿼리입니다. Linq를 Using해야 사용할 수 있습니다.
using System.Collections.Generic;
using System.Linq;
public class QueryTest
{
public static void Start()
{
int[] scores = { 90, 71, 82, 93, 75, 85 };
IEnumerable<int> scoreQuery =
from score in scores
where score > 85
orderby score descending
select score;
foreach (int Result in scoreQuery)
{
Console.WriteLine(Result);
}
string[] PhoneNum = { "010-4617-0000", "010-5555-0000", "011-8728-0000" };
IEnumerable<string> queryFirstNum =
from ph in PhoneNum
let editedNum = ph.Replace("0000", "0167")
select editedNum;
foreach (string str in queryFirstNum)
{
Console.WriteLine(str);
}
}
}[/code]데이터베이스에서 쓰이는 그 쿼리입니다. Linq를 Using해야 사용할 수 있습니다.
from절로 시작하여 select절 혹은 group절로 끝납니다. 근데 솔직히 그룹은 잘 모르겠네요 -_-;;;
select절은 책의 설명은 '다른 모든 형식의 시퀀스를 생성할 수 있다' 고 되어 있는데 그냥 쿼리 결과를 반환해서 쿼리변수에 넣는 것 같습니다. 솔직히 DB쪽도 예전에 Access할 때 잠깐 했던게 전부라서 잘 모르겠지만;;;
아, 뭐... 돌려보실 분은 직접 Start함수를 Main으로 바꾸신 뒤 컴파일하시면 되고요 간단하게 제 생각을 말하자면.... -_- DB가 아닌 사용자 데이터 형식(배열이나, 배열, 혹은 배열같은거...)에 쿼리를 사용하는건 foreach를 쓰는 것과 크게 다르지 않아 보이네요. 물론 단순한 용도로 쓰일 때만이지만요. foreach의 한계가 있으니...
[code csharp] foreach (string str in PhoneNum)
{
Console.WriteLine(str.Replace("0000", "0167"));
}
[/code]위의 예제에서 아래부분은 이런식으로 간단히 출력할 수도 있지요. 궂이 쿼리를 쓰지 않고도요. 하지만 쿼리를 쓰면 바뀐 번호가 저장된 쿼리변수가 생긴다는 점이 있으니 확실히 쿼리가 쓸 곳이 많겠지요.
Trackback 0 And
Comment 4
Prev
