csharp linq query using the same syntax for different data sources

In the previous article, we discussed that a linq query has consistent pattern for different data sources. The next obvious question is whether a query in LINQ can have the same syntax and be used against different data sources such as collections, databases, and even XML documents. Further, can we query against multiple data sources in a single operation using linq. Study the following linq query syntax.

var queryByCountry =
      from cust in customers
      group cust by cust.Country;




Let us assume that we have our customer data including country details, in a collection or in a database or in a XML file. When we use a query with the above LINQ syntax it retrieves all the names of the customers grouped by country, which ever is the data source. To know where we specify the data source, refer this article titled: structure of a linq query. The above query example shows that we can use a linq query with a particular syntax against multiple data sources.