.NET Tips and Tricks

Blog archive

Using LINQ with Collections that Don't Support LINQ, Revisited

In an earlier tip, I discussed how the OfType operator lets you run LINQ queries against collections that don't otherwise support LINQ. One reader of that column pointed out that, under the hood, a function named Cast was doing all the work. Another reader pointed out that what's special about OfType and Cast is that, unlike the other LINQ keywords, they work with collections that aren't generic types. Instead, OfType and Cast allow you to specify the type of the objects in the collection.

As an example of how to use Cast and OfType, imagine an ArrayList holding nothing but Customer objects:

Dim customers As New ArrayList
customers.Add(New Customer)
customers.Add(New Customer)

Both Cast and OfType will let you issue LINQ queries against the collection:

Dim custs = From c In customers.Cast(Of Customer)()
            Where c.CreditLimit = 0
            Select c

Dim custs = From c In customers.OfType(Of Customer)()
            Where c.CreditLimit = 0
            Select c

But what if the ArrayList contains a variety of objects? This collection contains both DeadbeatCustomer and PremiumCustomers:

customers.Add(New DeadbeatCustomer)
customers.Add(New PremiumCustomer)

Either Cast or OfType will let you work with this collection of diverse objects, but in very different ways. OfType will extract the objects of a specific type, so this example will process only the DeadbeatCustomer objects in the collection:

Dim custs = From c In customers.OfType(Of DeadbeatCustomer)()
            Where c.CreditLimit = 0
            Select c

Cast, on the other hand, will let you work with all items in the collection, provided they share an interface (i.e., inherit from some common base object or implement a common interface). This example assumes that both DeadbeatCustomer and PremiumCustomer interfaces both implement an interface named IBuyer:

Dim custs = From c In customers.Cast(Of IBuyer)()
            Where c.CreditLimit = 0
            Select c

So thanks, guys: This was your column. I'll send you a bill for writing it up.

Posted by Peter Vogel on 02/04/2014 at 3:23 AM


comments powered by Disqus

Featured

  • 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.

  • AWS Open Sources Tool for Porting .NET Framework Apps to .NET Core

    Leading cloud computing platform Amazon Web Services open sourced the it announced in July for helping users port old .NET Framework applications to the new .NET Core framework.

  • Uno Platform Ports Windows Calculator to Linux

    Uno Platform has ported the famed Windows Calculator, open sourced last year, to Linux as part of a continuing "proof point" effort to demonstrate the reach of what it describes as the sole UI offering available to target Windows, WebAssembly, iOS, macOS, Android and Linux with single-codebase applications coded in C# and XAML.

Upcoming Events