IQueryable member methods not behaving?
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 man开发者_如何学Pythony 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?
Like all LINQ methods, the Union
method returns a new IQueryable<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 new IQueryable<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:
list = list.Union(a.ToList());
精彩评论