MSDN Magazine - September 2008 - (Page 40) Figure 10 Using Concat, Union, Intersect, Except ' From SimpleSetDemo in the sample: Concatenates two sequences, taking ' all members of each. Union removes duplicates. Dim db As New SimpleDataContext ' Create two sequences: Dim group1 = db.Customers.Select(Function(cust) cust.Country).Take(5) Dim group2 = db.Customers.Select(Function(cust) cust.Country) _ .Skip(30).Take(5) ' Show both groups: sw.WriteLine("group1:" & GetCommaList(group1)) sw.WriteLine("group2:" & GetCommaList(group2)) ' Return the complete list of countries: Dim group3 = group1.Concat(group2) ' Return a unique list of countries in the two groups: group3 = group1.Union(group2) ' Find all elements in group1 that are also in group2: Can supply custom ' IEqualityComparer instance to compare items if they're not simple ' types: group3 = group1.Intersect(group2) ' Find the differences between group1 and group2. That is, return the set ' of all items that are in the first group but not the second. This is ' equivalent to calculating the union of the two, and removing the ' contents of the intersection of the two. Can supply custom ' IEqualityComparer instance to compare items if they're not simple ' types: group3 = group1.Except(group2) Results group1: Germany, Mexico, Mexico, UK, Sweden group2: France, France, France, France, Germany group1.Concat(group2): Germany, Mexico, Mexico, UK, Sweden, France, France, France, France, Germany group1.Union(group2): Germany, Mexico, UK, Sweden, France group1.Intersect(group2): Germany group1.Except(group2): Mexico, UK, Sweden To call Aggregate, supply a “seed” value (that is, the initial value of the calculated result) and a calculating function that accepts two parameters: the first contains the current total value, and the second contains the specific item to be aggregated. Within the function, perform the calculation. For example, to sum the squares of the difference between the UnitPrice and the mean price, you could create an aggregating function like this: Function(current As Decimal, item As Product) _ current + CDec((item.UnitPrice - averagePrice) ^ 2)) bilities. The first example, Figure 10, shows the Enumerable.Concat, Enumerable.Union, Enumerable.Intersect, and Enumerable.Except methods. Each method operates on a pair of sequences. The Concat and Union methods seem similar, combining the results of two sequences. However, the Concat method adds the output of one sequence to another, while the Union method removes duplicates from the result. The Intersect method returns a sequence with all the items common to both input sequences, and the Except method returns all items that are in the first sequence but not in the second (in other words, the difference between the two sequences). Running the code in Figure 10 produces the output at the bottom of the figure (the output first displays the two sequences, then the results of the calculations). Note that all the samples you see here use simple, default comparers. If you want to perform set operations on sequences of more complex objects, you can call the overloaded versions of the methods that accept instances of custom comparers, as you’ve seen in previous examples. The Enumerate class also supports several more complex set operations, using the Enumerable.Join, Enumerable.GroupBy, and Enumerable.GroupJoin methods. I will describe and demonstrate each of these methods in a moment. Imagine that some method hands you two sequences that are related, and you want to join them based on the correlation of a key value in both sequences. Obviously, this task is more typically thought of as being something for a relational database to handle. However, you can use the Enumerable.Join method to do this same kind of work. Figure 11 joins a sequence containing categories with a sequence containing products. To call the Join method, you must supply a function that returns the primary key in the parent sequence, a function that returns the foreign key in the child sequence, and a function that projects the data from the two sequences into the output sequence. (If the correlation key isn’t a simple type, you must also supply a custom comparer, as you’ve seen previously, to compare the key values.) Figure 11 Joining Two Sequences ' From ComplexSetOperationDemo in the sample: Dim categories = _ db.Categories.Where(Function(cat) cat.CategoryID = 3) ' If the correlation key isn't a simple type, you could ' provide a comparer to compare the two key values: Dim result = categories.Join(db.Products, _ Function(cat) cat.CategoryID, _ Function(prod) prod.CategoryID, _ Function(cat, prod) _ New With {.CategoryName = cat.CategoryName, _ .ProductName = prod.ProductName}) sw = New StringWriter For Each item In result sw.WriteLine("{0} -- {1}", item.CategoryName, item.ProductName) Next The code in Figure 9 calculates the standard deviation. You can use the Aggregate method on non-numeric values as well. You previously saw an example that cast a list as an array of String values, so that you could use the String.Join method. You can accomplish the same goal using the Aggregate method: ' From AggregateDemo in the sample: Dim db As New SimpleDataContext Dim customers = _ From cust In db.Customers _ Where cust.Country = "France" _ Select cust.ContactName ' Note that the seed value is an empty string: Dim customerNames = customers.Aggregate(String.Empty, _ Function(current, name) _ If(String.IsNullOrEmpty(current), name, current & ", " & name)) Results Confections -- Pavlova Confections -- Teatime Chocolate Biscuits Confections -- Sir Rodney’s Marmalade ‘ More items here. . . Confections -- Tarte au sucre Confections -- Scottish Longbreads Performing Set Operations The Enumerable class provides methods that perform set operations, such as calculations of unions and intersections. This final section investigates the methods of the class that provide these capa40 msdn magazine Advanced Basics
Table of Contents Feed for the Digital Edition of MSDN Magazine - September 2008 MSDN Magazine - September 2008 Contents Toolbox CLR Inside Out Data Points Advanced Basics Office Space Cutting Edge Hierarchy ID New Features for Microsoft SQL Server 2008 Prism Data Services Advanced WPF Test Run Security Briefs Foundations { End Bracket } MSDN Magazine - September 2008 MSDN Magazine - September 2008 - (Page Intro) MSDN Magazine - September 2008 - Contents (Page Cover1) MSDN Magazine - September 2008 - Contents (Page Cover2) MSDN Magazine - September 2008 - Contents (Page 1) MSDN Magazine - September 2008 - Contents (Page 2) MSDN Magazine - September 2008 - Contents (Page 3) MSDN Magazine - September 2008 - Contents (Page 4) MSDN Magazine - September 2008 - Contents (Page 5) MSDN Magazine - September 2008 - Contents (Page 6) MSDN Magazine - September 2008 - Contents (Page 7) MSDN Magazine - September 2008 - Contents (Page 8) MSDN Magazine - September 2008 - Contents (Page 9) MSDN Magazine - September 2008 - Contents (Page 10) MSDN Magazine - September 2008 - Toolbox (Page 11) MSDN Magazine - September 2008 - Toolbox (Page 12) MSDN Magazine - September 2008 - Toolbox (Page 13) MSDN Magazine - September 2008 - Toolbox (Page 14) MSDN Magazine - September 2008 - Toolbox (Page 15) MSDN Magazine - September 2008 - Toolbox (Page 16) MSDN Magazine - September 2008 - Toolbox (Page 17) MSDN Magazine - September 2008 - Toolbox (Page 18) MSDN Magazine - September 2008 - CLR Inside Out (Page 19) MSDN Magazine - September 2008 - CLR Inside Out (Page 20) MSDN Magazine - September 2008 - CLR Inside Out (Page 21) MSDN Magazine - September 2008 - CLR Inside Out (Page 22) MSDN Magazine - September 2008 - CLR Inside Out (Page 23) MSDN Magazine - September 2008 - CLR Inside Out (Page 24) MSDN Magazine - September 2008 - CLR Inside Out (Page 25) MSDN Magazine - September 2008 - CLR Inside Out (Page 26) MSDN Magazine - September 2008 - Data Points (Page 27) MSDN Magazine - September 2008 - Data Points (Page 28) MSDN Magazine - September 2008 - Data Points (Page 29) MSDN Magazine - September 2008 - Data Points (Page 30) MSDN Magazine - September 2008 - Data Points (Page 31) MSDN Magazine - September 2008 - Data Points (Page 32) MSDN Magazine - September 2008 - Data Points (Page 33) MSDN Magazine - September 2008 - Data Points (Page 34) MSDN Magazine - September 2008 - Advanced Basics (Page 35) MSDN Magazine - September 2008 - Advanced Basics (Page 36) MSDN Magazine - September 2008 - Advanced Basics (Page 37) MSDN Magazine - September 2008 - Advanced Basics (Page 38) MSDN Magazine - September 2008 - Advanced Basics (Page 39) MSDN Magazine - September 2008 - Advanced Basics (Page 40) MSDN Magazine - September 2008 - Advanced Basics (Page 41) MSDN Magazine - September 2008 - Advanced Basics (Page 42) MSDN Magazine - September 2008 - Advanced Basics (Page 43) MSDN Magazine - September 2008 - Advanced Basics (Page 44) MSDN Magazine - September 2008 - Office Space (Page 45) MSDN Magazine - September 2008 - Office Space (Page 46) MSDN Magazine - September 2008 - Office Space (Page 47) MSDN Magazine - September 2008 - Office Space (Page 48) MSDN Magazine - September 2008 - Office Space (Page 49) MSDN Magazine - September 2008 - Office Space (Page 50) MSDN Magazine - September 2008 - Office Space (Page 51) MSDN Magazine - September 2008 - Office Space (Page 52) MSDN Magazine - September 2008 - Cutting Edge (Page 53) MSDN Magazine - September 2008 - Cutting Edge (Page 54) MSDN Magazine - September 2008 - Cutting Edge (Page 55) MSDN Magazine - September 2008 - Cutting Edge (Page 56) MSDN Magazine - September 2008 - Cutting Edge (Page 57) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 58) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 59) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 60) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 61) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 62) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 63) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 64) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 65) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 66) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 67) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 68) MSDN Magazine - September 2008 - New Features for Microsoft SQL Server 2008 (Page 69) MSDN Magazine - September 2008 - Prism (Page 70) MSDN Magazine - September 2008 - Prism (Page 71) MSDN Magazine - September 2008 - Prism (Page 72) MSDN Magazine - September 2008 - Prism (Page 73) MSDN Magazine - September 2008 - Prism (Page 74) MSDN Magazine - September 2008 - Prism (Page 75) MSDN Magazine - September 2008 - Prism (Page 76) MSDN Magazine - September 2008 - Prism (Page 77) MSDN Magazine - September 2008 - Prism (Page 78) MSDN Magazine - September 2008 - Prism (Page 79) MSDN Magazine - September 2008 - Data Services (Page 80) MSDN Magazine - September 2008 - Data Services (Page 81) MSDN Magazine - September 2008 - Data Services (Page 82) MSDN Magazine - September 2008 - Data Services (Page 83) MSDN Magazine - September 2008 - Data Services (Page 84) MSDN Magazine - September 2008 - Data Services (Page 85) MSDN Magazine - September 2008 - Data Services (Page 86) MSDN Magazine - September 2008 - Advanced WPF (Page 87) MSDN Magazine - September 2008 - Advanced WPF (Page 88) MSDN Magazine - September 2008 - Advanced WPF (Page 89) MSDN Magazine - September 2008 - Advanced WPF (Page 90) MSDN Magazine - September 2008 - Advanced WPF (Page 91) MSDN Magazine - September 2008 - Advanced WPF (Page 92) MSDN Magazine - September 2008 - Advanced WPF (Page 93) MSDN Magazine - September 2008 - Advanced WPF (Page 94) MSDN Magazine - September 2008 - Advanced WPF (Page 95) MSDN Magazine - September 2008 - Advanced WPF (Page 96) MSDN Magazine - September 2008 - Test Run (Page 97) MSDN Magazine - September 2008 - Test Run (Page 98) MSDN Magazine - September 2008 - Test Run (Page 99) MSDN Magazine - September 2008 - Test Run (Page 100) MSDN Magazine - September 2008 - Test Run (Page 101) MSDN Magazine - September 2008 - Test Run (Page 102) MSDN Magazine - September 2008 - Test Run (Page 103) MSDN Magazine - September 2008 - Test Run (Page 104) MSDN Magazine - September 2008 - Security Briefs (Page 105) MSDN Magazine - September 2008 - Security Briefs (Page 106) MSDN Magazine - September 2008 - Security Briefs (Page 107) MSDN Magazine - September 2008 - Security Briefs (Page 108) MSDN Magazine - September 2008 - Security Briefs (Page 109) MSDN Magazine - September 2008 - Security Briefs (Page 110) MSDN Magazine - September 2008 - Security Briefs (Page 111) MSDN Magazine - September 2008 - Security Briefs (Page 112) MSDN Magazine - September 2008 - Foundations (Page 113) MSDN Magazine - September 2008 - Foundations (Page 114) MSDN Magazine - September 2008 - Foundations (Page 115) MSDN Magazine - September 2008 - Foundations (Page 116) MSDN Magazine - September 2008 - Foundations (Page 117) MSDN Magazine - September 2008 - Foundations (Page 118) MSDN Magazine - September 2008 - Foundations (Page 119) MSDN Magazine - September 2008 - { End Bracket } (Page 120) MSDN Magazine - September 2008 - { End Bracket } (Page Cover3) MSDN Magazine - September 2008 - { End Bracket } (Page Cover4)
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.