다음을 통해 공유


체인 쿼리 예제(C#)(LINQ to XML)

이 예제는 지연 실행 예제 예제를 기반으로 하며 지연된 실행과 지연 평가를 사용하는 두 쿼리를 함께 연결하면 어떻게 되는지 보여 줍니다.

예: yield return을(를) 사용하여 실행을 연기하는 두 번째 확장 메서드 추가

이 예제에서는 소스 컬렉션의 모든 문자열에 지정된 문자열을 추가한 다음 변경된 문자열을 생성하는 AppendString 다른 확장 메서드가 도입되었습니다.

public static class LocalExtensions
{
    public static IEnumerable<string>
      ConvertCollectionToUpperCase(this IEnumerable<string> source)
    {
        foreach (string str in source)
        {
            Console.WriteLine("ToUpper: source >{0}<", str);
            yield return str.ToUpper();
        }
    }

    public static IEnumerable<string>
      AppendString(this IEnumerable<string> source, string stringToAppend)
    {
        foreach (string str in source)
        {
            Console.WriteLine("AppendString: source >{0}<", str);
            yield return str + stringToAppend;
        }
    }
}

class Program
{
    static void Main(string[] args)
    {
        string[] stringArray = { "abc", "def", "ghi" };

        IEnumerable<string> q1 =
            from s in stringArray.ConvertCollectionToUpperCase()
            select s;

        IEnumerable<string> q2 =
            from s in q1.AppendString("!!!")
            select s;

        foreach (string str in q2)
        {
            Console.WriteLine("Main: str >{0}<", str);
            Console.WriteLine();
        }
    }
}

이 예제는 다음과 같은 출력을 생성합니다.

ToUpper: source >abc<
AppendString: source >ABC<
Main: str >ABC!!!<

ToUpper: source >def<
AppendString: source >DEF<
Main: str >DEF!!!<

ToUpper: source >ghi<
AppendString: source >GHI<
Main: str >GHI!!!<

이 예제에서 각 확장 메서드가 소스 컬렉션의 항목마다 한 번씩 작동하는 것을 확인할 수 있습니다.

이 예제에서 알아 두어야 할 점은 컬렉션을 생성하는 쿼리를 연결했지만 중간 컬렉션이 구체화되지 않는다는 것입니다. 대신 각 항목이 한 지연 메서드에서 다음 지연 메서드로 전달됩니다. 이렇게 하면 먼저 문자열 배열을 하나 가져온 다음 대문자로 변환된 두 번째 문자열 배열을 만들고 마지막으로 각 문자열 끝에 느낌표가 추가된 세 번째 문자열 배열을 만드는 방법보다 훨씬 적은 메모리를 사용합니다.

이 자습서의 다음 문서에서는 중간 구체화를 보여 줍니다.

참고 항목