Santosh Benjamin's Weblog

Adventures with AppFabric, BizTalk & Clouds

LINQ-ed Lists

leave a comment »


If you’re still staying on the fringes of LINQ (quite like me 🙂 ) , here’s something I found that got me liking it quite a bit more and maybe it will do the same for you. I like LINQ in small doses. I’ve seen horrible complex expressions that I would never understand even if i lived a million years, and I dont want to write such code, so I only use it where it makes expressing intent concise and preferably, in one simple line.   (Read on only if you are not a LINQ expert).

So I’ve got a (contrived) example here that resembles closely a few problems I needed to solve recently. The 3 or 4 main scenarios I was faced with are as follows  (all operating on a list of complex objects).

  • Remove a set of entries (specific criteria) from somewhere in the list
  • Check if the list contains all the specified objects
  • Finding an object with a specific attribute (pretty much a subset of any of the 3 scenarios above).

Now as you can imagine,  removing entries from custom collections is not a trivial task. Iterating through collections and deleting them causes collection modification errors as the system tries to deal with the changing length of the collection and so on. Even finding an item in a custom collection is several lines of a foreach. So here’s how to do all this in a trivial way in LINQ.  (Its possible there are even more concise ways to do this , so feel free to enlighten me (as long as it doesnt involve an IQueryable<> that joins to an IEnumerable<> and magically projects something into the universe, blah, blah etc 🙂 , see I know the lingo!)

Assume a class named Customer (what else ?) with the following structure

class Customer
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public Customer(int id, string name)
        {
            this.Id = id;
            this.Name = name;
        }
    }

Now lets assume I create a List<> of these customer objects named Customers with values such as (1,Name1), (2,Name2), (3,Name3) and so on.

Remove all customers with name = Name2

customers.RemoveAll(c => c.Name.Equals(“Name2”));

Check if the list contains all the given entries

List<string> expectedReferences = new List<string> { “Name1”, “Name2”, “Name3”, “Name4”, “Name5” };

var references = from c in customers select c.Name;

Console.WriteLine(expectedReferences.SequenceEqual(references));

Finding an entry with a specific criteria

var result = from c in customers where c.Name.Equals(“Name2”) select c;

.. and so on. If your custom object implements the Equals property then you can do even more funky stuff like returning the Intersection (common elements) of the lists or the Except (distinct elements) with single lines of code. I thought the RemoveAll() and SequenceEqual() methods were particularly fascinating because of the amount of code they reduce. I immediately put this to work in the project and in MockingBird where I had a number of places where I was doing object searches.

So that’s it on the subject of LINQ for now. Definitely not intended to be a tutorial. Just something I found and it helped me quite a bit.

Advertisements

Written by santoshbenjamin

August 3, 2010 at 10:31 PM

Posted in Coding, General, LINQ

Tagged with

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: