A simple question leads to the thinking of IEnumerable and IQueryable

Source: Internet
Author: User

Problem Overview:

First of all, there is a Customer table and the Customer owner table relationship is many-to-many, access to the database using the EF so here we open the lazy load, the need is to each customer's owner of all the comma concatenation in the owner of this column,

It's so easy for you to see what you're looking for. If you are writing SQL maybe we will go with two loops, the problem is to use LINQ.

This is the beginning of my writing:

First of all, the type of resultlist received is supposed to be iqueryable<customer>, but we're going to put in the Principalnames field (comma-stitched display field) so we're receiving an anonymous type. The error of writing the report is:

Approximate meaning: LINQ entities do not recognize methods of the system. String connection (System. System. String[]) string, the method that this method cannot be translated into a storage expression

Then various Baidu Google found its reason is to translate this LINQ into SQL when the string. The Join () method is not recognized and cannot be translated, and I am depressed and even skeptical of LINQ

Can you achieve such a demand,

Halfway to think about it. Do not use LINQ implementation, with two loop assignment to achieve implementation: the practice is to cycle the customer information recycling customers under the head of the owner of the name of the stitching assigned to the customer's head

But since the previous customer is anonymous type principalnames does not exist in the Customer table so it cannot be assigned, so the solution is also to build a view model with Principalnames field

In order to be able to loop and then assign values! I am also drunk, how this trouble.

I think it's impossible. That's a normal demand, isn't it? How can linq not be realized, continue to find ~

In the blog Park to find an article explaining IEnumerable and IQueryable differences: http://www.cnblogs.com/FlyEdward/archive/2010/02/01/Linq_ExpressionTree2.html

Feel a bit of meaning to continue to find similar articles in support, found that this is really the thing in the mischief, first look at the correct query wording:

The point is, where I'm going to draw the red line. Resultlist receive type is ienumberable<customer> type so write it will first check all the data into memory in the query, and do not

The following condition, which is the anonymous class, includes a string. The Join method is translated into SQL so that it does not appear until the string is not present. Join () method does not recognize the problem, OK to solve!

Summary :

IQueryable inherit from IEnumerable, so they are not different for data traversal.

1.IEnumerable queries must be executed locally. And we have to load all the data locally before executing the query. And more often. The data that is loaded has a lot of data that we don't need. But we have to transfer more data.
With IEnumerable, all the filtering, sorting, and other operations for IEnumerable occur in memory. This means that the data has been fetched into memory from the database and is only filtered and sorted in memory.

2.IQueryable can always only provide the data you need. Significantly reduced data transfer
The advantage of IQueryable is that it has an expression tree, and all the filtering, sorting, and other operations for IQueryable are cached in the expression tree, and the expression tree is performed by Iqueryprovider to get the data operation only when the real traversal occurs.

The above view is a personal understanding, if there are problems welcome to correct.

A simple question leads to the thinking of IEnumerable and IQueryable

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.