Entity Framework to learn the primary chapter 3--LINQ to Entities

Source: Internet
Author: User

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());


}

}

}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.