In the previous example, because the data source is an array, it implicitly
supports the generic
IEnumerable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/9eekhta0(printer).aspx ] interface.
This fact means it can be queried with LINQ. A query is executed in a
foreach statement, and
foreach requires
IEnumerable [
http://msdn2.microsoft.com/en-us/library/h1x9x1b1(printer).aspx ] or
IEnumerable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/9eekhta0(printer).aspx ] . Types that
support
IEnumerable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/9eekhta0(printer).aspx ] or a derived
interface such as the generic
IQueryable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/bb351562(printer).aspx ] are called
queryable types.
A queryable type requires no modification or special treatment to serve as a
LINQ data source. If the source data is not already in memory as a queryable
type, the LINQ provider must represent it as such. For example, LINQ to XML
loads an XML document into a queryable
XElement [ http://msdn2.microsoft.com/en-us/library/bb340098(printer).aspx ]
type:
// Create a data source from an XML document.
// using System.Xml.Linq;
XElement contacts = XElement.Load(@"c:\myContactList.xml");
With LINQ to SQL, you first create an object-relational mapping at design time
either manually or by using the
Object Relational Designer (O/R Designer) [
http://msdn2.microsoft.com/en-us/library/bb384429(printer).aspx ] . You write
your queries against the objects, and at run-time LINQ to SQL handles the
communication with the database. In the following example,
Customer represents a specific table in the database, and
Table<Customer> supports generic
IQueryable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/bb351562(printer).aspx ] , which
derives from
IEnumerable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/9eekhta0(printer).aspx ] .
// Create a data source from a SQL Server database.
// using System.Data.Linq;
DataContext db = new DataContext(@"c:\northwind\northwnd.mdf");
For more information about how to create specific types of data sources, see the
documentation for the various LINQ providers. However, the basic rule is very
simple: a LINQ data source is any object that supports the generic
IEnumerable<(Of
<(T>)>) [
http://msdn2.microsoft.com/en-us/library/9eekhta0(printer).aspx ] interface, or
an interface that inherits from it.