IEnumerable 在 foreach 循环中未按预期更新(使用 C#、MVC2 和 Linq)
我有三个表 Tbl_Listings、Tbl_AttributesLU 和 Tbl_ListingAttributes。
Tbl_Listings 包含我的所有列表。 Tbl_AttributesLU 保存许多属性(带有 ID 的名称) Tbl_ListingAttributes 保存每个列表的许多属性。
我希望允许我的网站的用户搜索列表,同时能够通过每个列表附加的属性缩小搜索范围。
为此,我首先将用户选择的所有属性作为列表发送到我的 ViewModel - 称为 AttributesFromView。
然后,我获取所有列表并将它们放入 IEnumarable 集合中 - 称为 ListingsInDB。
然后,我创建一个 foreach 循环,尝试根据 AttributesFromView 中的每个属性缩小列表范围。以下是代码。
IEnumerable<Listing> ListingsInDB = from x in DBEntities.Tbl_ListingSet select x;
foreach (int Attribute in AttributesFromView)
{
ListingsInDB = (from x in ListingsInDB
from a in x.Tbl_ListingAttributes
where a.Tbl_AttributesLU.ID == Attribute
select x);
}
现在,因为我的 ListingsInDB 集合位于 foreach 循环之外,所以我假设在 foreach 循环的每次迭代中,它应该通过仅选择附加了特定属性的列表来缩小集合范围。因此,在第一次迭代中,它将选择所有具有 AttributesFromView[0] 的列表。然后(在下一次迭代中),从这个新更新的 ListingsInDB 集合中,它将选择具有 AttributesFromView[1] 等的所有其他列表...
但是,这不起作用。相反,它将始终选择 AttributesFromView 列表中具有最后一个属性的项目。我对为什么会发生这种情况有点困惑,非常感谢您帮助解决该问题。
另外,很抱歉标题非常模糊 - 我真的不知道如何表达这个问题。
预先感谢,
希菲
I have three tables Tbl_Listings, Tbl_AttributesLU and Tbl_ListingAttributes.
Tbl_Listings holds all of my listings.
Tbl_AttributesLU holds a number of attributes (a name with an ID)
Tbl_ListingAttributes holds a number of attributes for each listing.
I want to allow users of my site to search for listings while being able to narrow down the search by the attributes attached to each listing.
To do this I first send all the attributes selected by the user to my ViewModel as a list - called AttributesFromView.
Then, I get all my listings and put them in an IEnumarable collection - called ListingsInDB.
I then create a foreach loop trying to narrow down the list based on each attribute in AttributesFromView. The following is the code.
IEnumerable<Listing> ListingsInDB = from x in DBEntities.Tbl_ListingSet select x;
foreach (int Attribute in AttributesFromView)
{
ListingsInDB = (from x in ListingsInDB
from a in x.Tbl_ListingAttributes
where a.Tbl_AttributesLU.ID == Attribute
select x);
}
Now because my ListingsInDB collection sits outside the foreach loop, I'm assuming that on each iteration of the foreach loop, it should narrow the collection by only selecting the listings with the particular attributes attached. So on the first iteration, it would select all the listings that have AttributesFromView[0]. Then (in the next iteration), from this newly updated ListingsInDB collection it would select all further listings that have AttributesFromView[1] and so on...
This however does not work. Instead, it will always select the items with the last attribute in the AttributesFromView List. I'm a little confused as to why this is happening and would really appreciate your help in resolving the issue.
Also, apologies for the title being very vague - I really did not know how to phrase this question.
Thanks in advance,
Sheefy
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在关闭循环变量,即 被认为是有害的。
解决此问题的一种方法是复制循环变量的值:
You are closing over the loop variable, which is considered harmful.
One way to fix it is to make a copy of the value of the loop variable: