리스트의 List<(Of <(T>)>)..::.Find 메서드 또는 FindAll 등은 대리자 메소드 입니다.
알고나면 정말정말 유용하게 사용가능!
Find 는 리스트에서 결과값을 찾음 빠져나가고 FindAll은 찾고나서도 끝까지 검색합니다.
예제
리스트형식이 string 형이면 string 형으로 반환합니다 그형에 맞게 반환 합니다.
그러므로 클래스 등등 다 찾을수 있습니다 ㅎ
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");리스트를 생성합니다.
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
EndsWithSaurus 를 사용해서 검색을 합니다.
private static bool EndsWithSaurus(String s)
{
if (s == "Triceratops")
{
return true; //찾으면 true 리턴
}
return false;
}찾으면 true를 반환하고 있습니다.
아래와 같이 다양한 조건을 검색 할 수 있습니다.
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
전체소스
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TESTConsoleApplication
{
class Program
{
static void Main(string[] args)
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(EndsWithSaurus));
Console.ReadLine();
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
}
}
만약에 "saurus" 를 변경하고 싶다면?
변수를 넘겨야 하는데 넘길수가 없따; 무명대리자 이용 뭐 전역변수로 해도 되긴된다 ㅋ
방법
dinosaurs.Find(
delegate(string s) { //무명대리자 이용
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() ==searchopt))
{
return true;
}
else
{
return false;
}
}
)); using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Drawing;
namespace TESTConsoleApplication
{
public class Program
{
static void Main(string[] args)
{
List<string> dinosaurs = new List<string>();
dinosaurs.Add("Compsognathus");
dinosaurs.Add("Amargasaurus");
dinosaurs.Add("Oviraptor");
dinosaurs.Add("Velociraptor");
dinosaurs.Add("Deinonychus");
dinosaurs.Add("Dilophosaurus");
dinosaurs.Add("Gallimimus");
dinosaurs.Add("Triceratops");
string searchopt="saurus";
Console.WriteLine("\nFind(EndsWithSaurus): {0}",
dinosaurs.Find(
delegate(string s) { //무명대리자 이용
if ((s.Length > 5) && (s.Substring(s.Length - 6).ToLower() ==searchopt))
{
return true;
}
else
{
return false;
}
}
));
Console.ReadLine();
}
// Search predicate returns true if a string ends in "saurus".
private static bool EndsWithSaurus(String s)
{
if ((s.Length > 5) &&
(s.Substring(s.Length - 6).ToLower() == "saurus"))
{
return true;
}
else
{
return false;
}
}
}
}
'LINQ & Entity' 카테고리의 다른 글
| [LINQ TO SQL] DataContext에서 조인하기 (0) | 2010.02.23 |
|---|---|
| Lambda Expression Example (0) | 2010.02.23 |
| Using LINQ to SQL (0) | 2010.02.23 |
| LINQ 로 Like 검색하기 (0) | 2010.02.19 |
| DataView 를 LINQ 로 검색하기 (0) | 2010.01.27 |
| [LINQPad] LINQ 쿼리 빌더 및 확인 TOOL (0) | 2009.12.18 |
| LINQ 쿼리를 이용한 데이터 엑세스 (0) | 2009.12.18 |
| [DataContext.GetCommand ]LINQ 로 만들어진 쿼리를 확인해보기 (0) | 2009.12.18 |
| Linq Pager 페이징 (0) | 2009.12.08 |
| [자주쓰는 쿼리] LINQ 쿼리 식(C# 프로그래밍 가이드) 예제 (0) | 2009.12.02 |