programming4us
programming4us
ENTERPRISE

LINQ to Objects : How to Join with Data in Another Sequence (part 6) - One-to-Many Joins - The join Operator

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019

3. One-to-Many Joins

A one-to-many join allows a subgroup collection of related elements to a parent element in another sequence. The most common example of a one-to-many join is a listing of Customers, each with a collection of Orders. The same caveat applies to one-to-many joins as warned in one-to-one joins. LINQ to Objects will not perform joins as well as a relational database system like SQL Server. Joining data is what DBMSs are good at, and although you can perform these joins in LINQ to Objects, it should be reserved for small data volumes. Do the heavy lifting in SQL Server and fine-tuning in LINQ to Objects.

There are a number of ways to achieve one-to-many joins using LINQ, and these include

  • Using the join/into combination (or GroupJoin) operator

  • Using a subquery in the select projection

  • Using the ToLookup operator

Each of these approaches are covered in detail next, including their performance behavior, but Table 4 summarizes the different performance traits for each syntax when working with many records at one time or only joining to a single record.

Table 4. One-to-Many Join Techniques and Their Performance Comparisons
TechniqueMany outer record performanceSingle outer record performanceComments
Using the join/into combination (or GroupJoin) operatorGenerally poor; better as inner sequence size growsWorstWhen run over one million iterations, this performed slower than both other options. Unless trying to stay syntactically similar to SQL, use either a subquery or lookup technique. This technique is more effective than the subquery technique as the inner sequence size grows. Break even at around five records. 
Using a subquery in the select projectionGood for small inner sequences only or when ordering of the inner sequence is importantBestThis technique was only marginally slower than the lookup technique. The lookup technique traverses the inner sequence only once; this technique will traverse the inner sequence multiple times. Use this technique when you need to order the inner sequence elements.
Using the ToLookup operatorBestGoodIf performance is a key requirement, then use this technique.

The identical sample data used in the previous section on one-to-one joins (Table 2) will continue to be used throughout this section unless otherwise specified.

The join/into (GroupJoin) Operator

A small variation of the query expression join syntax allows joined records to be captured in a subgroup

Listing 17 demonstrates how to achieve a one-to-many group relationship using the join and into keyword combination. The Console output is shown in Output 7. The into keyword captures each group, and this group can be accessed in the select projection and subsequently accessed as a property of each result element.

Listing 7. One-to-Many Join using the join/into keyword combination—see Output 7
var customers = Customer.SampleData();
var orders = Order.SampleData();

var q1 = from c in customers
join o in orders on
c.CustomerID equals o.CustomerID
into cust_orders
select new
{
LastName = c.LastName,
Orders = cust_orders
};

foreach (var customer in q1)
{
Console.WriteLine("Last name: {0}", customer.LastName);
foreach (var order in customer.Orders)
Console.WriteLine(" - Order number: {0}", order.OrderNumber);
}

Output 7.
Last name: Gottshall
- Order number: Order 1
- Order number: Order 2
Last name: Valdes
Last name: Gauwain
Last name: Deane
- Order number: Order 3
- Order number: Order 4
Last name: Zeeman
- Order number: Order 5
Other  
  •  Moving into SAP Functional Development : Gaining Control of Change Control - How to Organize and Plan for Change in the Real World
  •  Moving into SAP Functional Development : Gaining Control of Change Control - Change Control and the SAP Solution Stack
  •  Moving into SAP Functional Development : Gaining Control of Change Control - Change Control Affects Everything
  •  Exchange Server 2010 : Outlook Integration (part 7) - Document Library Integration
  •  Exchange Server 2010 : Outlook Integration (part 6) - Alert Integration
  •  Exchange Server 2010 : Outlook Integration (part 5) - Task Integration
  •  Exchange Server 2010 : Outlook Integration (part 4) - Contact Integration
  •  Exchange Server 2010 : Outlook Integration (part 3) - Creating a Meeting Workspace
  •  Exchange Server 2010 : Outlook Integration (part 2) - Calendar Integration
  •  Exchange Server 2010 : Outlook Integration (part 1) - Integration Overview
  •  
    Top 10
    Free Mobile And Desktop Apps For Accessing Restricted Websites
    MASERATI QUATTROPORTE; DIESEL : Lure of Italian limos
    TOYOTA CAMRY 2; 2.5 : Camry now more comely
    KIA SORENTO 2.2CRDi : Fuel-sipping slugger
    How To Setup, Password Protect & Encrypt Wireless Internet Connection
    Emulate And Run iPad Apps On Windows, Mac OS X & Linux With iPadian
    Backup & Restore Game Progress From Any Game With SaveGameProgress
    Generate A Facebook Timeline Cover Using A Free App
    New App for Women ‘Remix’ Offers Fashion Advice & Style Tips
    SG50 Ferrari F12berlinetta : Prancing Horse for Lion City's 50th
    - Messages forwarded by Outlook rule go nowhere
    - Create and Deploy Windows 7 Image
    - How do I check to see if my exchange 2003 is an open relay? (not using a open relay tester tool online, but on the console)
    - Creating and using an unencrypted cookie in ASP.NET
    - Directories
    - Poor Performance on Sharepoint 2010 Server
    - SBS 2008 ~ The e-mail alias already exists...
    - Public to Private IP - DNS Changes
    - Send Email from Winform application
    - How to create a .mdb file from ms sql server database.......
    programming4us programming4us
    programming4us
     
     
    programming4us