Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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);     
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
376 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...