Also, if the user of that method would need to specify further predicates, those also be executed in the database, which makes it way faster. And Use Iqueryable and Ienumurator when you want to get dumb data as an collection of objects, it will come back as a loose type collection and no restrictions applied.
Also, using a list will give you the ability to add, sort and convert layer into Array, Ienumurator or as Queryable. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Ask Question. Asked 10 years, 9 months ago. Active 1 year, 3 months ago. Viewed 53k times. Improve this question. Community Bot 1 1 1 silver badge. Add a comment. Active Oldest Votes.
Customers where c. AsEnumerable where c. First ; This simple change has a serious consequence. Edit after comments I just want to be clear about the distinction between when things are done client-side and when they're done server-side. WriteLine cust. Improve this answer. Adam Robinson Adam Robinson k 31 31 gold badges silver badges bronze badges. Adam Robinson - So is that better allowing them to get a returned query that still has not hit the database and when they need a list they can iterate it through and then it hits the db source.
Or is better to just give them a list straight of the bat? In some cases, it may be advantageous to allow the user to further refine the query, especially in cases where you may have complex query logic that is common to several different queries. While this isn't saying that you should always do this, returning IQueryable with the base complex logic in place would allow you to reuse that logic in different places without duplicating it in code — Adam Robinson.
Adam Robinson -What would happen in this scenario. I return a IQueryable list but my view needs uses IEnumerator. I would cast it to this would there be some that problem you talked about with Enumerable? Is that bad if it does all the querying on the client side. What I am trying to do is have a viewmodel that is sent to the view that contains a IEnumerable to go through. This simple change has a serious consequence.
Up until now, we've only talked about IQueryable and IEnumerable. This is because they are similar, complimentary interfaces. In both cases, you're defining a query ; that is, you're defining where to find the data, what filters to apply, and what data to return.
Both of these are queries. Like we've talked about, the first query is using IQueryable and the second uses IEnumerable. In both cases, however, this is just a query. Defining the query doesn't actually do anything against the data source. The query is actually executed when code begins to iterate over the list. This can happen multiple ways; a foreach loop, calling ToList , etc. The query is executed the first and every time it's iterated. If you were to call ToList on query two times, you would end up with two lists with completely distinct objects.
They might contain the same data, but they would be different references. I just want to be clear about the distinction between when things are done client-side and when they're done server-side. I first construct a query based on FirstName. What's going to happen here is that the first query will be evaluated, running this SQL:.
So we're going to bring back everyone whose FirstName starts with "Ad". Note that there's nothing in here about LastName. That's because it's being filtered out client-side.
Once it brings back these results, the program will then iterate over the results and deliver only the records whose LastName starts with "Ro". The downside to this is that we brought back data--namely, all rows whose LastName doesn't start with "Ro" --that could have been filtered out on the server. IEnumerable describes behavior, while List is an implementation of that behavior.
When you use IEnumerable , you give the compiler a chance to defer work until later, possibly optimizing along the way. If you use ToList you force the compiler to reify the results right away.
Consider this:. Now you have a method that selects an initial sample "AllSpotted" , plus some filters. So now you can do this:. So is it faster to use List over IEnumerable? Therefore finding data is easy just by pointing out to its key. We have different data structure and stack is one of them.
Stack is subset of data structure. Stack is a prioritized data structure such as List is indexed base. Stack defines priority for each item, it means stack behavior forces its items to put push inside stack prioritized form.
So stack put later item on the top of items and this behavior is "defining priority for each item". Therefore whenever you want to insert item, you should add PUSH it at the top of the stack and whenever you want to remove POP item from stack, you should remove it from top of the stack. Queue is another kind of data structure that defines priority for each item in other form.
Therefore, whenever you want to insert item, you should add Enqueue it at the head of the queue and whenever you want to remove Dequeue item from queue, you should remove it from bottom of the queue. Since List is not Fixed Length makes developers feel flexible to use it, and because of it is Strongly Typed when it is defined "Generic" so our code runs fast at runtime because it does not need to wait for type definition.
Why do we need IList?
0コメント