IQueryable 成员方法没有行为?
这是我的代码:
IQueryable<Car> list = new List<Car>().AsQueryable<Car>();
foreach (TreeNode node in DataHierarchyTree.CheckedNodes)
{
var a = from c in ContextDB.Cars
where c.CarID == Int32.Parse(node.Value)
select c;
list.Union(a.ToList());
}
CarGridView.DataSource = list;
CarGridView.DataBind();
这不会呈现任何内容。我已经通过停止点运行了它,并且迭代了 5 次。如果我检查 a
值,则会生成一条 SELECT 语句,当与检查的节点列表中的值一起使用时,该语句会在结果集中生成大量行。
问题是,无论我单步执行代码时产生多少结果,list
始终为空。
这是为什么?我应该怎样做才能获得我正在寻找的汽车列表?
This is my code:
IQueryable<Car> list = new List<Car>().AsQueryable<Car>();
foreach (TreeNode node in DataHierarchyTree.CheckedNodes)
{
var a = from c in ContextDB.Cars
where c.CarID == Int32.Parse(node.Value)
select c;
list.Union(a.ToList());
}
CarGridView.DataSource = list;
CarGridView.DataBind();
This renders nothing. I have run it through with stop points and it iterates 5 times. If I inspect the a
value there is a SELECT statement generated which produces plenty of rows in the result set when used with the values in the checked nodes list.
The problem is that no matter how many results are produced when I step the through code, the list
is always empty.
Why is this and what should I do to get the list of cars I'm looking for?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
与所有 LINQ 方法一样,
Union
方法返回一个新的IQueryable
,其中包含联合结果。它不会修改原始实例。
因此,当您编写
list.Union(a.ToList())
时,您正在创建一个包含所有汽车的新IQueryable
,但不会执行任何操作它。列表变量不会改变。
您需要将新序列分配给
list
变量,如下所示:Like all LINQ methods, the
Union
method returns a newIQueryable<T>
containing the results of the union.It does not modify the original instance.
Therefore, when you write
list.Union(a.ToList())
, you are creating a newIQueryable<Car>
containing all of the cars, but not doing anything with it.The list variable doesn't change.
You need to assign the new sequence to the
list
variable, like this: