views:

161

answers:

4

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);     
A: 

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.

Sruly
IEnumerable is also deferred execution.
Baddie
`IEnumerable` can be used for deferred execution too. For example, see [`Enumerable.Reverse`](http://msdn.microsoft.com/en-us/library/bb358497.aspx). It returns an `IEnumerable<TSource>`, but the actual reversal isn't started until the first element is enumerated.
Matthew Flaschen
+4  A: 

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.

Mike
Actually if possible, the `var` keyword should used in this case, then you don't have to worry about casting the type anywhere. Just change it to `var z = ...` without the `ToList()` at the end. This will probably keep it `IQueryable` but that inherits from `IEnumerable` anyway.
Andrew Koester
+3  A: 

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

jfar
A: 

The difference between IEnumerable and IQueryable that deserves to be highlighted is that an IQueryable object executes its underlying LINQ query as it's accessed. So if you pass an IQueryable around your application, you may be hitting the underlying data source more often than you expect.

Once you've retrieved and manipulated the data you need as an IQueryable, I recommend you return it as an IEnumerable, using, for example, IQueryable.ToList().

As has been mentioned, a List is just a particular implementation of IEnumerable; one that allows its items to be inserted/removed/referenced by index. Compare to, say, a Dictionary, which is also an IEnumerable, but is based around a hash table; its items are represented by key-value pairs.

djacobson
"IQueryable object executes its underlying LINQ query as it's accessed" - This doesn't make sense and is misleading. LINQ is just a language features which compiles to extension methods. Deferred execution only is possible when expression trees and a supported provider are used.
jfar
Fair enough. I'm thinking of LINQ to SQL in particular, where an underlying SQL statement is being executed against the database when the expression tree is evaluated. My impression was that access to this sort of IQueryable should be done carefully, lest that underlying database access occur more times, or in different places in the code, than you expect. Is that a misrepresentation of how deferred exec using the LINQ-to-SQL provider works?
djacobson