Entity Framework Integrated Application

Source: Internet
Author: User

1,LINQ Introduction

The full name Language Integrated query, which is a collection of languages, is integrated into the. NET language, which can be used in languages such as C#,VB. Supports querying data sources, including generic. NET objects, such as arrays, collections, XML, relational databases

can be divided into LINQ to objects. LINQ to XML, LINQ to Datasets, LINQ to SQL, and LINQ to Entities technology.

The internal mechanism of LINQ implementations is complex, but it is very handy for developers who use it, often using query expressions that require little code to implement, data source queries, complex filtering, sorting, and grouping.

Let's take a look at its syntax.

From variable

In Data source object

Where Condition expression

Order order sequence [ascending][descending]

Group Group conditions

into temp identifier

Select Selection Column

See, is not very similar to SQL syntax, but the use of similar, the mechanism is still very different.

The "1" LINQ to Objects has three types of operands, strings, generic collections, and normal collections.

Let's take a look at examples

LINQ to String

Code:

String name = "Zhangsan";
var k= from N in name
where n== ' z ' filters each char with Z
Select N; The n here represents a character
foreach (var item in k)
{
Console.WriteLine (item);
}

The result is Z

LINQ to List<t>

list<string> lists = new List<string> {
"Zhang San", "Li 31", "Zhang 32", "Li 33"
};
var k = from Z in lists
Where Z.startswith ("Zhang")//The first char in the filter string has a sheet.
Select Z;
foreach (var item in k)
{
Console.WriteLine (item);
}
The result is three sheets of 32,

LINQ to ArrayList is similar to generics and is interesting to see specific

But what kind of object supports LINQ queries?

Generally as long as the object is to see whether the implementation of the Ienumerable<t> interface, if implemented can certainly be used.

query expressions and Query methods

A simple comparison of a string

String Name= "Zhangsan";

query expressions

from N in name

Select N;

Query method

Name. Select (n=>n); a LAMDA expression is passed here.

In many cases, query methods and query expressions are mixed, of course, a lot of query methods, detailed view of the official documents, here is just point out how to use.

The "2" LINQ to Entities is actually the various collection objects contained in the DbContext object as the data source, which correspond to the type dbset<t> or ICOLLECTION<T>, both of which support LINQ operations. LINQ to Entities and to objects are similar in query coding, but they are very different in principle.

using (mydbentities db=new mydbentities ()) {//preceded by new out data source context

Var k=from s in Db.users//from the database to get the users table, here just get a filter statement, and do not really execute, only when the data inside to perform database operations, can not worry about efficiency issues

Where S.name.startswith ("Zhang")

Select S;

}

In the actual LINQ to Entities query expression, if there is a column of data in the database as a time type, if a query such as through where S.time>datetime.parse ("1990-4-4") will be an exception, because a storage problem is involved , only the canonical function can be used to convert to the corresponding store function. The corresponding table, please look for increased learning ability.

Querying multiple table data

In entity Frameword, navigation properties are generated based on the primary foreign key relationship in the relational database, and the navigation property is defined by default as the virtual property.

As long as the relational database relationship is complete, the query data can be used to make multiple tables.

However, if there is no such relationship between tables, you must use the Join connection to query the

Grammar

var k= from S in db.users//user table

Join G in db.usertypes//associated user type table

On S.typeid equals g.typeid//Association condition

Select New {s.name,g.name}//identifies the user name, and the user type name

There is a different way of using variables and not using variables in the choice of superior performance in the Entity Framework

As the first type of

var k= from S in db.users

Where s.name== "Zhang San"

Select S;

var kk=k.tolist ();

The second Kind

String name= Zhang San ";

var k= from S in db.users

where S.name==name

Select S;

var kk=k.tolist ();

Performance optimization

The second performance is better than the first because the second underlying is executed by calling the stored procedure, while the first one is recompiled at execution time and the stored procedure is precompiled only once. It is recommended to use the second method.

Context. Configuration.autodetectchangesenabled =false;//This statement is used to not enable the automatic state tracking feature

Enabling automatic tracing will cause the add () operation to consume a lot of performance, causing DbContext to traverse all cached entry, comparing their original values and their current values.

Local data cache, refers to the temporary file Exchange area, the temporary storage of certain data somewhere, time to take again, so more efficient.

Cases:

Db.users.Count ();//Here you have it.

Then next time use Db.users.Local.Count ();

     

Entity Framework Integrated Application

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.