CODEKILLER

반응형

Linq - Select, SelectMany
Linq - Select, SelectMany

public class Example
{
    public static void Main()
    {
        Console.Write("<< Linq - Select, SelectMany 출력 예제 >> \n");

        List<Samples> samples = new()
        {
            new Samples { LstTestString = new List<string> { "no1", "no2", "no3", "no4" }},
            new Samples { LstTestString = new List<string> { "no5", "no6", "no7" }},
            new Samples { LstTestString = new List<string> { "no8", "no9", "no10", "no11", "no12" }},
            new Samples { LstTestString = new List<string> { "no13", "no14", "no15", "no16" }}
        };

        IEnumerable<List<string>> query1 = samples.Select(fl => fl.LstTestString);

        IEnumerable<string> query2 = samples.SelectMany(fl => fl.LstTestString);

        Console.WriteLine(" --------  Select():");
        // Select.
        foreach (IEnumerable<String> collection in query1)
            foreach (string item in collection)
                Console.WriteLine(item);

        // SelectMany
        Console.WriteLine("\n -------- SelectMany():");
        foreach (string item in query2)
            Console.WriteLine(item);

        Console.WriteLine(System.Environment.NewLine);
        Console.WriteLine("Press any key to exit");
        Console.ReadKey();
    }
}

class Samples
{
    public List<string> LstTestString { get; set; }
}
반응형

공유하기

facebook twitter kakaoTalk kakaostory naver band