LINQ Technology (LINQ to Entities) enables developers to create flexible, strongly typed queries directly from the development environment for Entity Framework object contexts by using LINQ Expressions and LINQ standard query operators. LINQ to Entities queries use the object service infrastructure. The ObjectContext class is the main class that interacts with the Entity Data model as a CLR object. Developers construct generic ObjectQuery instances through ObjectContext. The ObjectQuery generic class represents a query that returns an instance or collection of typed entities. The returned entity object is available for updating and is in the object context. The following is the process of creating and executing a LINQ to Entities query:
1. Construct ObjectQuery instance from ObjectContext.
2. Write LINQ to Entities queries in C # or Visual Basic by using the ObjectQuery instance.
3. Converts the LINQ standard query operators and expressions to the command tree.
4. A query that executes a command tree representation of a data source. Any exceptions that are thrown on the data source during execution are passed directly to the client.
5. Return the query results to the client.
First, Linq to entities simple query
The following describes a simple LINQ to Entities query, which can use either an expression or a method based syntax. The testdriver.net used in this section is tested with Nunit2.4.
1, projection
The code is as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Objects;
using NUnit.Framework;
namespace NorthWindModel
{
[TestFixture]
public class TestEFModel
{
[Test]
public void Select()
{
using (var edm = new NorthwindEntities())
{
//基于表达式的查询语法
ObjectQuery<Customers> customers = edm.Customers;
IQueryable<Customers> cust1 = from c in customers
select c;
Assert.Greater(cust1.Count(), 0);
//使用ObjectQuery类的ToTraceString()方法显示查询SQL语句
Console.WriteLine(customers.ToTraceString());
}
}
}
}