LINQ to Services (LINQ to Amazon, google, bing, JSON, LDAP, Streams, DryadLINQ …)

Posted: February 21, 2011 in DotNet

THE HIDDEN PART OF LINQ -you will discover an amazing abilities of LINQ- 

LINQ to Services

In the real world, many widely used services already have LINQ implementations. Most of these implementations are community based and are not officially supported by single-service providers.

One of the first LINQ wrappers to an external service was LINQ to Amazon. Using this service,you can look for books in the Amazon catalog, filtering them by attributes such as publisher,price, title,and so on. Similarly, by using LINQ to Flickr (http://linqflickr.codeplex.com), you can search for photos stored in the Flickr photo-sharing service. LINQ to Flickr also offers support for adding or deleting photos, a feature not directly tied to the LINQ query syntax, but which is conceptually similar to the entities you can manage in LINQ to SQL or LINQ to Entities.

 Google is another source for accessing data through remote services. The LINQ to Google implementation actually supports only queries to Google Base, which is a search engine that can index structured entities decorated with customizable attributes.

 The presence of these attributes makes the use of LINQ meaningful, because you can include them as part of a LINQ query, as shown in the following example:

GoogleItems.GoogleContext gc = new GoogleItems.GoogleContext(key);

var r = from car in gc.products

where car.Brand == “car” && car.Price < 5400

select new { car.Title, car.Price };

 The Bing search engine offers a LINQ to Bing provider, which is part of the Web Application Toolkit for Bing Search. This provider supports searches on webpages, news, images, and video. Here is an example of a Bing web

 query:

Linq2Bing lb = new Linq2Bing();

var query = from w in lb.Web

where w.Text == “test” && w.Text == “test1”

select w;

 Yet another LINQ implementation that could be useful for accessing remote services is LINQ to JSON, which is included in Microsoft Silverlight and accessible through the System.Json namespace. Another implementation you can use without Silverlight is the Json.NET 3.5 library. This implementation provides a LINQ-integrated model that simplifies access from a .NET Framework application to data serialized with JSON.

 Simplifying access to remote services that provide integration with LINQ queries is an area that will likely see great expansion in the future. The service provider does not need to define its own LINQ wrapper; any service consumer can write a LINQ wrapper for that service. However,the most widely used services will probably be supported either directly by service providers or by projects that are supported by communities.

 LINQ for System Engineers

 Applications that control an existing infrastructure or that automate administrative tasks might be able to take advantage of LINQ while accessing specific services and APIs useful for their needs.

 LINQ to Active Directory is an implementation that simplifies access to Active Directory APIs, such as System.DirectoryServices (.NET) and ActiveDs (COM).

 The same author originally wrote (and analytically described) a more general implementation of LINQ to LDAP.

 Another interesting LINQ implementation is one that provides access to Windows Management Instrumentation (WMI). check implementation of LINQ to WMI . By using a provider like this, you can query WMI counters in a simple and declarative way.

 Dynamic LINQ

 The standard result of a LINQ query is considered to be static. It does not change after query execution. However, there are situations in which the definition of a LINQ query should be more dynamic, acting more as a filter in a stream of data or making the query result “live.”

 The LINQ to Streams implementation focuses on streaming data those changes in real time. Currently, it is a simple prototype, but the approach seems interesting for particular scenarios such as network monitors, financial services, and real-time data acquisition.

 Another approach to the same issue is the one used in SyncLINQ. This implementation is focused more on data binding over LINQ queries. To support dynamic updates of the query result, a SyncLINQ query returns collections that implement INotifyCollectionChanged, which is the interface provided by the .NET Framework to notify listeners of dynamic changes. The SyncLINQ implementation should establish compatibility with existing user interface components that support INotifyCollectionChanged.

 These implementations do not provide a solution for every tier of an application’s architecture, but they do present an interesting approach to solving some of the issues regarding the use of a common programming pattern (LINQ queries). These implementations attempt to simplify and encapsulate the work necessary to handle dynamic updates to data returned from a query.

 Other LINQ Enhancements and Tools

 DryadLINQ is a research project that uses LINQ to access Dryad, a distributed execution engine. DryadLINQ compiles lambda expressions into DLLs and routes the DLLs to remote machines for execution. This is a more complex form of parallelism than the one offered by PLINQ and is an interesting implementation for very CPU-intensive applications.

 Indexed LINQ, also known as I4O, is a class library through which you can create indexes on objects queried using LINQ to Objects. This implementation is particularly interesting for applications that load and maintain a large number of objects in memory. Using optimizations like the one provided by I4O, any object graph in memory can seem like an in-memory database, even if LINQ and I40 do not automatically solve the issues of concurrent access in a multithreaded environment.

 LINQExtender is a tool for building LINQ providers. The LINQ to Flickr provider mentioned earlier was created using this tool. The tool is particularly interesting because it provides an extensible model that supports an object-tracking service similar to the one that exists in LINQ to SQL. Another feature of LINQExtender is that it does not require you to extend a parser for an expression tree. The existing parser converts an expression tree into a simpler object that describes the query itself. This simplification might not be good for every application, but you will probably encounter many cases in which a simple provider cannot support complex queries just because the service that it wraps has very simple and limited query semantics.

 MetaLinq, also called LINQ to Expressions, is a library that allows manipulation of a LINQ query. This library offers an object model through which you can manipulate an expression tree (which could be a query tree) without the limitations imposed by a real expression tree, which is made of immutable objects. The final stage of this manipulation is the creation of an equivalent immutable expression tree that can be used with LINQ.

 A useful tool for writing and testing LINQ queries is LINQPad, which is both an interactive editor and an execution environment for LINQ and SQL queries. It supports queries written using LINQ to SQL, LINQ to Objects, and LINQ to XML.

 Another interesting tool is LINQ over C# , which provides a parser for Microsoft Visual C# that can be used together with LINQ as a solid base for refactoring code and creating software quality assurance (QA) tools.

 Finally, LINQ to Geo is a LINQ implementation for querying geospatial data, and LINQ to Lucene (http://linqtolucene.codeplex.com) supports the Lucene Information Retrieval System, which is a free open-source search engine.

Comments
  1. Hey! This is my 1st comment here so I just wanted to give a quick shout out and tell you I truly enjoy reading your blog posts. Can you recommend any other blogs/websites/forums that go over the same subjects? Thank you so much!

Leave a comment