Wednesday, 15 October 2014

Using LINQ or Lambda Expressions to Entities - Entity Framework Query Options

LINQ to Entities
a. Create new entityframework(EF) called DefestyEntity;
b. Tables generated by EF are Customers, Orders, and so on;
c. Queries must be well defined at compile time;

Codes:
DafestyEntity ctx = new DafestyEntity();

var query = from x in ctx.Orders
                    where x.Customer.CustomerID == "Tom"
                    select x;
foreach (var x in query)
    Console.WriteLine("{0} {1}", x.OrderID, x.OrderDate);

LINQ Key Words
- from
  Specifies a data source and a range variable (similar to an iteration variable).
- where
  Filters source elements based on one or more Boolean expressions separated by logical AND and OR operators(&& or ||)
- select
  Specifies the type and shape that the elements in the returned sequence will have when the query is executed.
- group
  Groups query results according to a specified key value.
- into
  Provides an identifier that can serve as a reference to the results of a join, group or select clause.
- order by
  Sorts query results in ascending or descending order based on the default comparer for the element type.
- join
  Joins two data sources based on an equality comparison between two specified matching criteria.


Lambda Expressions
a. A lambda expression is an anonymous function that can contain expressions and statements, and can be used to create delegates or expression tree types.

Codes:
ctx.Orders.Where(x => x.CustomerID =="Tom");

No comments:

Post a Comment