FROM

Specifies the collection used in SELECT statements.

Syntax

FROM expression [ ,...n ] as C

Arguments

expression: Any valid query expression that yields a collection to use as a source in a SELECT statement.

Remarks

A FROM clause is a comma-separated list of one or more FROM clause items. The FROM clause can be used to specify one or more sources for a SELECT statement.

Single Data Source

The simplest form of a FROM clause is a single query expression that identifies a collection and an alias used as the source in a SELECT statement, as illustrated in the following example:
var esql = @"select p from Products as p";
var q = db.CreateQuery<Product>(esql);
The Products is member of the data context instance, and also can use the entity type name as the data source.
var esql = @"select p from NorthwinDemo.Product as p";
var q = db.CreateQuery<Product>(esql);
Because of the namespace of the data context instance will be auto imported, and the namespace is named 'NorthwinDemo' so it same as below:
var esql = @"select p from Product as p";
var q = db.CreateQuery<Product>(esql);

Multiple Data Source

var esql = @"select p, o from Product as p, Orders as o";
var q = db.CreateQuery<IDataRecord>(esql);

Inner Join

An INNER JOIN produces a constrained Cartesian product of the two collections, as illustrated in the following example:
FROM C AS c [INNER] JOIN D AS d ON e
Examples
var esql = @"select o.OrderId, d.UnitPrice 
             from Orders as o Inner Join 
             OrderDetails as d on o.OrderId == d.OrderId";
var q = db.CreateQuery(esql);
var esql = @"select o.OrderId, d.ProductId, p.UnitPrice 
                from Orders as o 
                    Inner Join OrderDetails as d on o.OrderId == d.OrderId
                    Inner Join Products as p on d.ProductId == p.ProductId";
var q = db.CreateQuery(esql);

Left Join

An Left JOIN query expression produces a constrained Cartesian product of the two collections, as illustrated in the following example:
FROM C AS c LEFT JOIN D AS d ON e
Examples
var esql = @"select o.OrderId, d.UnitPrice 
             from Orders as o 
                  left join OrderDetails as d on o.OrderId == d.OrderId";
var q = db.CreateQuery(esql);
var esql = @"select o.OrderId 
             from Orders as o 
                  left join OrderDetails as d on o.OrderId == d.OrderId
                  left join Products as p on d.ProductId == p.ProductId";
var q = db.CreateQuery(esql);
var esql = @"select o.OrderId, c.CategoryId, p.ProductId 
             from Orders as o 
                  left join OrderDetails as d on o.OrderId == d.OrderId
                  left join Products as p on d.ProductId == p.ProductId
                  left join Categories as c on p.CategoryId == c.CategoryId";
var q = db.CreateQuery(esql);

Nested Query

Example
var esql = @"select od.o.OrderID as OrderID
                from ( select o, d 
                       from Orders as o 
                       inner join OrderDetails as d on o.OrderID = d.OrderID ) as od";
var q = db.CreateQuery(esql);

Last edited Mar 9, 2013 at 6:57 AM by ansiboy, version 5

Comments

No comments yet.