.NET Tips and Tricks

Blog archive

Selecting Items from a Collection using Another Collection with LINQ Joins

Here's the problem: you have a collection of objects with lots of information in those objects (the "rich" objects). You also have a collection of objects that represent a selection your user made in your application's UI (the "selected" objects). Unfortunately, your "selected" collection is a different and less useful set of objects; you really want to work with the "rich" objects. So Your problem is finding all the matching objects from the "rich" collection, based on the objects in the "selected" collection.

A LINQ Join solves the problem. Joining the two collections together gives you a new collection of the "rich" objects that have a matching item in the "selected" collection. In the LINQ statement's Join/On clause, you just need to specify the properties to be used to join the two collections together.

The following code joins an Entity Framework collection of all of the users in the company (CompanyUsers -- the rich objects) with a group of selected users from the UI (SelectedUsers -- the selected objects). The resulting collection holds just the rich objects:
Dim usrs = From cu In context.CompanyUsers
           Join id In SelectedUsers On cu.ID Equals id
           Select cu

Posted by Peter Vogel on 07/08/2013 at 1:16 PM


comments powered by Disqus

Featured

  • .NET for Apache Spark Debuts in Version 1.0

    The open source project .NET for Apache Spark has debuted in version 1.0, finally vaulting the C# and F# programming languages into Big Data first-class citizenship.

  • In-App Reviews Come to Xamarin.Forms Android

    Android is playing a little catch-up to iOS regarding in-app review functionality, just now coming tp Microsoft's Xamarin.Forms implementation.

  • C# Slides in Usage Ranking of Programming Languages

    "The fact that C# lost three places in the ranking of language communities during the last three years is mostly explained by its slower growth compared to C/C++ and PHP."

  • Telerik UI for Blazor Updated

    Progress announced an update to its Telerik UI for Blazor components, targeting Microsoft's open source Blazor framework that lets C# coders create web apps without having to rely upon JavaScript.

  • Infragistics Unveils UI Components for Blazor

    Infragistics, specializing in third-party UI/UX controls and tools, unveiled a new offering targeting Blazor, Microsoft's red-hot open source framework that allows for C#-based web development instead of traditional mainstay JavaScript.

Upcoming Events