Falling in Love with LINQ - Part 4: Universal Query

by Larry Spencer Saturday, November 19, 2011 4:53 PM

Our series on LINQ has progressed to the point where we can reveal why it is so powerful: Microsoft has designed it as a universal query language.

LINQ providers have been written for everything from C# objects, to SQL databases, to XML, to Amazon's catalog, to Flickr. What makes this so great is that your code requires very little change to query one thing rather than another. To put it another way, if you know how to LINQ to SQL, you know 99% of what you need to know to LINQ to Flickr!

Recall the example from Part 1 of this series. It queried a CityList object for the largest cities:


return Cities
         .OrderByDescending(c => c.Population)


In Part 3, we did the same thing, but against a SQL Server database:

return context.Cities
         .OrderByDescending(c => c.Population)

Instead of calling our LINQ methods against the Cities object, we queried context.Cities (our database table). Other than that, the code is identical!

If you want to expose your own whatever-it-is to LINQ, it is even possible to write your own LINQ provider, although that's not an exercise for the faint of heart.

Next time, we'll talk about the variety of LINQ operations available.

Tags: ,

All | General | Talks

About the Author

Larry Spencer

Larry Spencer develops software with the Microsoft .NET Framework for ScerIS, a document-management company in Sudbury, MA.