IEnumerable的< T> VS的IList< T> VS IQueryable的< T> [英] IEnumerable<T> VS IList<T> VS IQueryable<T>

查看:211
本文介绍了IEnumerable的< T> VS的IList< T> VS IQueryable的< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新到MVC.net场景(和.net为此事),但似乎我找到了各种选项想填充数据名单的时候。就我而言,此刻,我想从项目选择查询填充列表和渲染的结果JSON输出,所以忍耐一下......

所以,我的ViewModel类是这样的:

  [序列化()]
公共类TFSquery
{
    公众诠释MsgUid {获得;组; }
    公开日期时间CreateStamp {获得;组; }
}
 

然后我想用我的查询输出填充它:

 名单,其中,TFSquery> Z =(味精的_DB.Msg
                    选择新{msg.MsgUID,msg.CreateStamp})了ToList()。
 

然后我会循环输出到我的列表中,使我可以再在我的Json返回字符串输出?当我使用列表 VS 的IEnumerable VS 的IQueryable ??

 返回JSON(新{结果= Z},JsonRequestBehavior.AllowGet);
 

解决方案

我的经验法则:

  • 使用列表时,你必须添加,删除或按索引引用的项目。

  • 使用的IQueryable 时,你必须反对它运行的即席查询。

  • 使用的IEnumerable 默认情况下。

看起来你已经在做,在数据库查询,所以我建议用最简单的版本:的IEnumerable 来简单地能够遍历结果

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

解决方案

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.

这篇关于IEnumerable的&LT; T&GT; VS的IList&LT; T&GT; VS IQueryable的&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆