Differences between LINQ and generic, ienumerable, and iqueryable: lambda expressions, and converting from developing to Entity Framework

Source: Internet
Author: User

I. Based on the generic type, the generic type is introduced in. NET Framework 2.0. You do not need to know more about generics to write queries. However, you may need to understand two basic concepts:

  1. When you create a generic collection class (such as List (of t) instance, you replace "T" with the type of the object contained in the list. For example, the string list is represented as list <string>, and the customer Object List is represented as list <customer>. The generic list is strongly typed and provides more benefits than storing its elements as a collection of objects. If you try to add the customer to the list <string>, an error occurs during compilation. The reason why generic collections are easy to use is that you do not have to perform forced type conversion during runtime.

  2. Ienumerable (of T) is an interface through which you can useForeachTo enumerate generic collection classes. Generic collection classes support ienumerable (of T), just like non-generic collection classes (such as arraylist) Support ienumerable.

 

 

Ii. Differences between ienumerable and iqueryable

All represent generic sets, but ienumerable <t> refers to local enumerated sets. For example

List <person> Persons = new list <person> // here is the set initialization {new person {age = 10, name = "Joey", sex = "male "}, // object initialization new person {age = 20, name = "etam", sex = "female"}, new person {age = 30, name = "Eric ", sex = "male"}, new person {age = 10, name = "King", sex = "male "}};

When we do not know what type to search for, we can use VaR

Ienumerable <person> P = from per in persons where per. name [0] = 'E' select per; var P2 = from per2 in persons where per2.name [0] = 'E' select per2; foreach (var a in P) {response. write (. age + "name is" +. name + "<br>");} foreach (var a in P2) {response. write (. age + "name is" +. name + "<br> ");}

 

Iqueryable <t> refers to remote query of LINQ 2 SQL

Ienumerable <t> that is, the LINQ 2 object stores data in the memory.

Iqueryable <t> is also a way of storing data in a remote server for both the LINQ 2 SQL statements. This is delayed execution.

 

Iii. Lambda is used as a parameter for standard query operator methods (such as where) in method-based LINQ queries.

List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" }; IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6); foreach (string fruit in query) { Console.WriteLine(fruit); } /* This code produces the following output: apple mango grape */

A Lambda expression is an anonymous function that can contain expressions and statements and can be used to create a delegate or expression tree type.

All lambda expressions use the lambda operator =>, which reads "goes ". The left side of the lambda operator is the input parameter (if any), and the right side contains the expression or statement block. Lambda expressions x => X * X are read as "X goes to X times X ".

 

Lambda expressions are further improved on the syntax form of anonymous methods in. net2.0. They are still described in the Code:

VaR instring = List. findall (delegate (string s) {return S. indexof ("yjinglee") >=0 ;});

Using lambda expressions is easier to understand.

VaR instring = List. findall (S => S. indexof ("yjinglee")> = 0 );

It can be seen that the lambda expression format is: (parameter list) => expression or statement Block

 

It seems that the development has been stopped for LINQ to SQL. If you want to use LINQ to SQL, you also need to get an object relationship designer (O/R designer) to map the demonstrated northwnd database.

After coming up with LINQ, the C # team made a lightweight ORM tool called LINQ to SQL to demonstrate what can be done by using this thing.

Then, this item is handed over from the C # team to the ADO. Net team. Ado. Net decided to do a bigger tool, namely the Entity Framework, and it stopped the development of LINQ to SQL. Of course, many people are still using LINQ to SQL.

However, LINQ is very powerful and can be used to query an array. Currently, the object framework uses

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.