Tuesday, February 15, 2011

About LINQ : Degenerate Queries

Degeneration is a law of nature. No seriously. But that's not what we are talking about here. We are talking about degenerate queries.

Here is a sample of a degenerate query:
from foo in collection select foo


Its called degenerate because it seemingly adds no value to the code e.g.:

var items = from foo in collection select foo;
foreach (var item in items)
{
      //Do something
}

We could have easily done (hypothetically):

foreach (var item in collection)
{
      //Do something
}

But it is required when you use linq to SQL ( or LINQ to EF or LINQ to XML etc) because these implementations of LINQ actually override the Select extension method. It looks degenerate ... but it is required for the API to hook in for expression evaluation. 

"A query that simply returns a result equal to the original data source is called degenerate query expression" 

Enjoy!