Just had the need to compare that a list was in the correct order for a test I’d written. Handily there is a Linq extension method that makes the job easy, as the following snippet shows.

private static bool
 OrdersMatch(IEnumerable newsItems, IEnumerable newsIds)
{
  return newsItems.Select(n => n.Id).SequenceEqual(newsIds);
}

You can use an extension method as I have, if you need to match on a specific part of the objects you are comparing.

LINQ Sytax

June 17, 2009

This post was going to be longer but I decided to cut it down, mainly due to the fact that it has been sat in my drafts for about 6 months. So in the end it’s a (very) short post about different LINQ to entity syntax, nothing to taxing but someone might find it useful.

As you may be aware LINQ provides various extension methods that allow you to perform query like operations (the ones we are interested in right now are Select, SelectMany and Where). This allows you to get data from any IEnumerable collection.

There are two syntaxes that you can use to get data from a collection.

The first is SQL like:

   1: var pilots = from tieFigher in tieFighters select tieFigher.Pilot;

Or using extension methods:

   1: pilots = tieFighters.Select(n => n.Pilot);

The second method uses the Select method which takes a Lamda Expression that allows us to specify what we want to select; in this case the Pilot. Both of these return the same result, and now that we have a list of pilot strings, we can iterate through them using foreach:

   1: foreach (var pilot in pilots)
   2: {
   3:   Console.WriteLine(pilot);
   4: }

So now we understand how to Select something but what would be really great, is if we could select a particular something. To do this is where we have to make use of the Where clause, again I will demonstrate both syntaxes.

The SQL like syntax:

   1: var pilots = from tieFighter in tieFighters
   2:     where tieFighter.Pilot == "Darth Maul"
   3:     select tieFighter;

And the equivalent would be:

   1: pilots = tieFighters.Where(n => n.Pilot == "Darth Vader");

As pointed out at the start this post has been dramtically cut down, I aim to publish something I bit more detailed soon.

Random LINQness!

September 11, 2008

This post was going to be longer but I decided to cut it down, mainly due to the fact that it has been sat in my drafts for about 6 months. So in teh end it’s a (very) short post about different LINQ to entity syntax, nothing to taxing but someone might find it useful.

As you may be aware LINQ provides various extension methods that allow you to perform query like operations (the ones we are interested in right now are Select, SelectMany and Where). This allows you to get data from any IEnumerable collection.

There are two syntaxes that you can use to get data from a collection.

The first is SQL like:

   1: var pilots = from tieFigher in tieFighters select tieFigher.Pilot;

Or using extension methods:

   1: pilots = tieFighters.Select(n => n.Pilot);

The second method uses the Select method which takes a Lamda Expression that allows us to specify what we want to select; in this case the Pilot. Both of these return the same result, and now that we have a list of pilot strings, we can iterate through them using foreach:

   1: foreach (var pilot in pilots)
   2: {
   3:   Console.WriteLine(pilot);
   4: }

So now we understand how to Select something but what would be really great, is if we could select a particular something. To do this is where we have to make use of the Where clause, again I will demonstrate both syntaxes.

The SQL like syntax:

   1: var pilots = from tieFighter in tieFighters
   2:     where tieFighter.Pilot == "Darth Maul"
   3:     select tieFighter;

And the equivalent would be:

   1: pilots = tieFighters.Where(n => n.Pilot == "Darth Vader");

As pointed out at the start this post has been dramtically cut down, I aim to publish something I bit more detailed soon.