Tải bản đầy đủ - 0 (trang)
16-6. Filter and Select from Multiple Data Sources

16-6. Filter and Select from Multiple Data Sources

Tải bản đầy đủ - 0trang

CHAPTER 16 ■ USING LINQ



The Code

The following example creates two data sources that share a common key, and uses the join keyword to

combine them in a LINQ query in order to create an anonymous result type that contains elements from

both data sources:

using

using

using

using



System;

System.Collections.Generic;

System.Linq;

System.Text;



namespace Apress.VisualCSharpRecipes.Chapter16

{

class Recipe16_6

{

static void Main(string[] args)

{

// Create the data sources.

IList colorsource = createColorData();

IList shelflifesource = createShelfLifeData();

// Perform the LINQ query with a join.

var result = from e in colorsource

join f in shelflifesource on e.Name equals f.Name

where e.Color == "green"

select new

{

e.Name,

e.Color,

f.Life

};

// Write out the results.

foreach (var element in result)

{

Console.WriteLine("Name: {0}, Color: {1}, Shelf Life: {2} days",

element.Name, element.Color, element.Life);

}

// Wait to continue.

Console.WriteLine("\nMain method complete. Press Enter");

Console.ReadLine();

}

static IList createColorData()

{

return new List()

{

new FruitColor("apple", "green"),

new FruitColor("orange", "orange"),

new FruitColor("grape", "green"),



763



www.it-ebooks.info



CHAPTER 16 ■ USING LINQ



new

new

new

new



FruitColor("fig", "brown"),

FruitColor("plum", "red"),

FruitColor("banana", "yellow"),

FruitColor("cherry", "red")



};

}

static IList createShelfLifeData()

{

return new List()

{

new FruitShelfLife("apple", 7),

new FruitShelfLife("orange", 10),

new FruitShelfLife("grape", 4),

new FruitShelfLife("fig", 12),

new FruitShelfLife("plum", 2),

new FruitShelfLife("banana", 10),

new FruitShelfLife("cherry", 7)

};

}

}

class FruitColor

{

public FruitColor(string namearg, string colorarg)

{

Name = namearg;

Color = colorarg;

}

public string Name { get; set; }

public string Color { get; set; }

}

class FruitShelfLife

{

public FruitShelfLife(string namearg, int lifearg)

{

Name = namearg;

Life = lifearg;

}

public string Name { get; set; }

public int Life{ get; set; }

}

}



764



www.it-ebooks.info



CHAPTER 16 ■ USING LINQ



Running the example gives the following results:

Name: apple Color green Shelf Life: 7 days

Name: grape Color green Shelf Life: 4 days



Main method complete. Press Enter



16-7. Use Permutations of Data Sources

Problem

You need to enumerate all permutations of two or more data sources.



Solution

Include more than one from statement in your LINQ query.



How It Works

You can enumerate through the permutations of multiple data sources by using more than one from

keyword in your LINQ query. The query will be applied to every permutation of every element in each

data source. The following fragment illustrates a query that uses from twice:

string[] datasource1 = { "apple", "orange",};

int[]

datasource2 = { 21, 42 };

var result = from e in datasource1

from f in datasource2

select new

{

e,

f

};

The select part of the query (and any filters that we might have applied) will be called for every

combination of element from the two data sources—apple and 21, apple and 42, orange and 21, and

orange and 42.



765



www.it-ebooks.info



CHAPTER 16 ■ USING LINQ



The Code

The following example creates two arrays and uses them as data sources for a LINQ query with multiple

from keywords. The result is an anonymous type containing the elements from both sources, and each

element in the result is printed to the console.

using

using

using

using



System;

System.Collections.Generic;

System.Linq;

System.Text;



namespace Apress.VisualCSharpRecipes.Chapter16

{

class Recipe16_7

{

static void Main(string[] args)

{

// Create the data sources.

string[] datasource1 = { "apple", "orange", "cherry", "pear" };

int[]

datasource2 = { 21, 42, 37 };

// Perform the LINQ

var result = from e

from f

select

{

e,

f

};



query.

in datasource1

in datasource2

new



// Print the results.

foreach (var element in result)

{

Console.WriteLine("{0}, {1}", element.e, element.f);

}

// Wait to continue.

Console.WriteLine("\nMain method complete. Press Enter");

Console.ReadLine();

}

}

}



766



www.it-ebooks.info



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

16-6. Filter and Select from Multiple Data Sources

Tải bản đầy đủ ngay(0 tr)

×