返回IQueryable<T>与IEnumerable<T>?有什么区别?
IQueryable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;
IEnumerable<Customer> custs = from c in db.Customers
where c.City == "<City>"
select c;
Run Code Online (Sandbox Code Playgroud)
两者都会延迟执行,何时应该优先于另一个?
我之前在这里发过这个问题但是我不满意我理解完整的后果.问题是返回类型应该是使用linq-to-sql的数据层返回以获得最大的灵活性和查询能力.这是我读过/发现的内容:
IEnumerable是有限的,只允许读取转发操作.IEnumerable是最通用的.我发现IEnumerable确实允许查询操作与扩展语法.
由于插入操作,List允许最大的灵活性.
应使用集合而不是列表来启用只读集合.
永远不应该使用IQueryable,它应该"使用和关闭".IQueryable不返回列表,但为数据库生成查询语法.
我觉得我对折衷有更好的感觉,但仍然不确定一些事情:
为什么我会选择具体类型的界面变体?即IList或ICollection与列表或集合.我会得到什么好处?
我看到扩展操作有效,但扩展的查询语法也能正常工作吗?
有人建议我之前使用AsQueryable().但是,如果我没有连接到数据库,为什么我会这样做呢?似乎扩展方法无论如何都有效.
我知道这可能是意见,但我正在寻找最佳实践.
据我所知,IQueryable<T>实现IEnumerable<T>,所以在我的DAL中,我目前有方法签名,如下所示:
IEnumerable<Product> GetProducts();
IEnumerable<Product> GetProductsByCategory(int cateogoryId);
Product GetProduct(int productId);
Run Code Online (Sandbox Code Playgroud)
我应该IQueryable<T>在这里使用吗?
这两种方法的优点和缺点是什么?
请注意,我打算使用Repository模式,所以我将有一个这样的类:
public class ProductRepository {
DBDataContext db = new DBDataContext(<!-- connection string -->);
public IEnumerable<Product> GetProductsNew(int daysOld) {
return db.GetProducts()
.Where(p => p.AddedDateTime > DateTime.Now.AddDays(-daysOld ));
}
}
Run Code Online (Sandbox Code Playgroud)
我应该改变我IEnumerable<T>的IQueryable<T>吗?一个或另一个有哪些优点/缺点?
使用IEnumerable<T>返回类型有问题吗?FxCop抱怨返回List<T>(它建议返回Collection<T>).
好吧,我一直受到一条规则的指导:"尽可能接受,但要回报最大值".
从这个角度来看,返回IEnumerable<T>是一件坏事,但是当我想使用"懒惰检索"时我该怎么办?此外,yield关键字是如此的好.
MVC.net场景的新手(以及.net),但是当我想要用数据填充"列表"时,我似乎找到了各种各样的选项.在我的情况下,我想从一个选择的项目查询填充一个列表,并用JSON渲染结果输出,所以请耐心等待....
所以,我的viewmodel类是这样的:
[Serializable()]
public class TFSquery
{
public int MsgUid { get; set; }
public DateTime CreateStamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
然后我想用我的查询输出填充它:
List<TFSquery> z = (from msg in _DB.Msg
select new { msg.MsgUID, msg.CreateStamp }).ToList();
Run Code Online (Sandbox Code Playgroud)
那么我会将输出循环到我的List中,以便我可以在我的Json返回字符串中输出吗?当我使用LIST VS IENUMERABLE VS IQUERYABLE?
return Json(new { Result = z }, JsonRequestBehavior.AllowGet);
Run Code Online (Sandbox Code Playgroud) 想象一下,我有一个SearchService层,它有一个方法来搜索以某个字符串开头的所有汽车;
public static class Searcher{
public IAnInterface<Car> CarsStartingWith(string startWith){
//magic
}
}
Run Code Online (Sandbox Code Playgroud)
我的服务应该使用什么界面?
IQueryable可以在我的应用程序的其余部分呈现一个漂亮的流畅界面.
IEnumerable有一个懒惰的方面.
IList是最实用的.
我想让我的所有服务返回相同的界面以保持一致性,使整个事情变得更加容易.
ICollection可能也是一个选项,但它只提供这么少......
由于我无法从Linq到Entities查询返回这些类型中的任何一种,因此我坚持返回List.
从DB调用返回时(在单独的WCF服务或DLL中),Controller中的代码失败,因为dbcontext连接已关闭.
请注意以下代码.对于IEnumerable和IQueryable,由于以上描述,数据不会返回.
// Controller
IEnumerable<ProjectDescription> projectDdl = db.GetProjectDropDownList().AsEnumerable();
// Error coming back because dbcontext connection was closed.
ViewBag.ProjectsCust = new SelectList(projectDdl, "ProjectId", "Name");
Run Code Online (Sandbox Code Playgroud)
// WCF Service or DLL
public IEnumerable<ProjectDescription> GetProjectDropDownList()
{
try
{
//IQueryable<ProjectDescription> project = null;
using (YeagerTechEntities DbContext = new YeagerTechEntities())
{
DbContext.Configuration.ProxyCreationEnabled = false;
DbContext.Database.Connection.Open();
IEnumerable<ProjectDescription> project = DbContext.Projects.Select(s =>
new ProjectDescription()
{
ProjectID = s.ProjectID,
Description = s.Description
}
);
return project;
}
}
catch (Exception ex)
{
throw ex; …Run Code Online (Sandbox Code Playgroud) c# ×5
.net ×3
linq-to-sql ×3
asp.net-mvc ×2
collections ×2
ienumerable ×2
iqueryable ×2
linq ×2
architecture ×1
datacontext ×1
enumeration ×1
json ×1
wcf ×1