473,606 Members | 2,218 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

IQueryable<T>, IEnumerable<T> or List<T>

Hello,

On my MVC projects I often create classes which contains properties
which are lists of other classes.

Should I start using IQueryable<Tor IEnumerable<Tin stead of
List<T>?

What are the differences and when should I use IQueryable<T>,
IEnumerable<Tor List<T>?

Can, and should, I use IQueryable<Tor IEnumerable<Tin my ViewData
properties instead of List<T>?

Thanks,
Miguel
Jul 8 '08 #1
1 8866
shapper wrote:
On my MVC projects I often create classes which contains properties
which are lists of other classes.

Should I start using IQueryable<Tor IEnumerable<Tin stead of
List<T>?
You probably shouldn't be using List<Tin any case, because it ties you to
one particular implementation. Collection<Tis more extensible, because it
allows you to replace the List<Twith a more specific derived class
allowing you to take action when the collection changes. This isn't possible
if you use List<T>, since it has no hooks for derived classes to alter
behavior. You can always use aggregation, but this is far less convenient.

Better still would be to use IList<Tor ICollection<Tin stead of any
particular class. Programming against interfaces offers the most flexibility
for you as an implementer. Clients miss out on some convenience methods if
you do this, however, and it also won't do if your collection has "special
needs" beyond accessing by index, so this needs to be balanced against use.

You should also make it clear whether your collection is mutable or not, and
enforce this. Offering any of IList<T>, List<Tor Collection<Texp oses
methods to modify the collection. If this is what you want, good (and you'll
probably want to extend Collection<Tto respond to changes), otherwise
there is a ReadOnlyCollect ion<Twrapper that ensures these methods will
fail. Interface-wise it's compatible with IList<T>, ICollection<Tan d
IEnumerable<T>, but not List<T(which is another reason not to use List<T>
in your interface).
What are the differences and when should I use IQueryable<T>,
IEnumerable<Tor List<T>?
IQueryable<T>: probably never. This is only really useful for query
providers. LINQ already contains extension methods for turning any
collection or enumerable into a queryable data source, so there's no point
to offering this specific interface as opposed to IEnumerable<T>. It doesn't
enable clients to do anything more.

IEnumerable<T>: very general and the least useful. The only operation your
clients will be able to perform is foreach, and querying through LINQ. If
this is all you need to offer clients, it'll do. Most LINQ operations will
require enumerating all elements, however, so this is of limited use if
clients will need to be able to use the ordering of elements or the fact
that it's sorted, without forcing them to make array or list copies every time.

List<T>: see above. It's not a good idea to expose it.

--
J.
Jul 8 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
11145
by: Ronald S. Cook | last post by:
From my business tier (class) I get back an IQueryable<Penof data. Here is my client code that works fine: PenClass penClass = new PenClass(); IQueryable<Penpens = penClass.SelectPens(); However, I want to then select a subset of this data from within the client. I tried this: IQueryable<PenpensSubset = from p in pens select p.PenId, p.PenNumber;
0
8015
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7951
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8094
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
6770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5465
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3930
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3977
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2448
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1296
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.