You often need to create sequences or collections with items taken from different data sources. If you consider the example in the previous “Grouping Operators” section, it would be interesting to create a collection of objects in which the category name is also available so that the result can be more human-readable. This is possible in LINQ using union operators (not to be confused with the union Set
operator keyword), which perform operations that you know as joining. To complete the following steps, simply recall the previously provided implementation of the Product
and Category
classes and the code that populates new collections of products and categories. The goal of the first example is to create a new sequence of products in which the category name is also available. This can be accomplished as follows:
The code is quite simple to understand. Both products
and categories
collections are queried, and a new sequence is generated to keep products and categories whose CategoryID
is equal. This is accomplished via the Join
keyword in which the On
operator requires the condition to be evaluated as True
. Notice that Join
does not accept the equality operator (=
), whereas it requires the Equals
keyword. In this case the query result is an IEnumerable(Of Anonymous type)
, but of course you could create a helper class exposing properties to store the result. You can then iterate the result to get information on your products, as in the following snippet:
The code produces the following output:
This is the simplest joining example and is known as Cross Join, but you are not limited to this. For example you might want to group items based on the specified key, which is known as Group Join. This allows you to rewrite the same example of the previous paragraph but taking advantage of joining can get the category name. This is accomplished as follows:
Notice that now the main data source is Categories
. The result of this query is generating a new sequence in which groups of categories store groups of products. This is notable if you take a look at the Select
clause, which picks sequences instead of single objects or properties. The following iteration provides a deeper idea on how you access information from the query result:
Such nested iteration produces the following output:
The Cross Join with Group Join technique is similar. The following code shows how you can perform a cross group join to provide a simplified version of the previous query result:
Notice that by simply providing a nested From
clause pointing to the group you can easily select what effectively you need from both sequences, for example the category name and the product name. The result, which is still a sequence of anonymous types, can be simply iterated as follows:
It produces the following output:
The last union operator is known as Left Outer Join. It is similar to the cross group join, but it differs in that you can provide a default value in case no item is available for the specified key. Consider the following code:
Notice the invocation of the Group.DefaultIfEmpty
extension method that is used with the If
ternary operator to provide a default value. You can then retrieve information from the query result as in the cross group join sample.
3.143.231.26