Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IEnumerable<T> VS IList<T> VS IQueryable<T>

New to the MVC.net scene (and .net for that matter), but seems I find a wide array of options when wanting to populate a "list" with data. In my case at the moment, I'd like to populate a list from a select query of items and render the results in JSON for output, so bear with me....

So, my viewmodel class is something like :

[Serializable()]
public class TFSquery 
{
    public int MsgUid { get; set; }
    public DateTime CreateStamp { get; set; }    
}

And then I'd like to populate it with my query output:

List<TFSquery> z = (from msg in _DB.Msg 
                    select new { msg.MsgUID, msg.CreateStamp }).ToList();

Then would I loop the output into my List so that I can then output in my Json return string? And when I use a LIST VS IENUMERABLE VS IQUERYABLE??

return Json(new { Result = z }, JsonRequestBehavior.AllowGet);     
like image 623
denisb Avatar asked Aug 30 '10 19:08

denisb


People also ask

What is the difference between IEnumerable T and IQueryable T?

The major difference between IQueryable and IEnumerable is that IQueryable executes query with filters whereas IEnumerable executes the query first and then it filters the data based on conditions.

Which is better IEnumerable or IList?

You use IEnumerable when you want to loop through the items in a collection. IList is when you want to add, remove, and access the list contents out of order.

What is difference between IEnumerable and IList?

IList doesn't support further filtering. IEnumerable exists in System. Collections Namespace. IEnumerable is a forward only collection, it can't move backward and between the items.

What is the difference between IQueryable IEnumerable IList ICollection?

IQueryable,IList,IDictionary,ICollection inherits IEnumerable Interface. All the interfaces of type collection will inherits IEnumerable Interface. Differences Between IEnumerable and IQueryable IEnumerable:-when you are running a query which returns of type IEnumerable.


3 Answers

My rules of thumb:

  • Use a List when you have to add, remove, or refer to an item by index.

  • Use a IQueryable when you have to run ad-hoc queries against it.

  • Use IEnumerable by default.

It looks like you're already doing the query "in" your database, so I'd suggest using the simplest version: IEnumerable to simply be able to loop through the results.

like image 181
Mike Avatar answered Sep 21 '22 19:09

Mike


If your new to .NET and C# I'd spend some time researching and becoming knowledgeable about what the different collection types are, how they differ, and when to use them. You'll use collections so often it you cannot afford to have a "simple one liner" summary understanding like the other answerers posted.

Here is a good guide on .NET collection types: http://msdn.microsoft.com/en-us/library/0ytkdh4s.aspx

IQueryable is its own special beast and deserves its own guide: http://msdn.microsoft.com/en-us/library/system.linq.iqueryable.aspx

like image 28
John Farrell Avatar answered Sep 22 '22 19:09

John Farrell


Each interface has its own set of uses.

IQueryable is for deferred queries (that is, it saves the query but only executes it when it is enumerated)

IEnumerable can be enumerated and that's it.

IList can have items added and removed.

like image 41
Sruly Avatar answered Sep 21 '22 19:09

Sruly