按整数值引用将覆盖我的 linq 查询
您知道为什么 val 的引用会在此查询中共享选择 ID 吗?
这是代码:
lst = new List<IQueryable<tblProduct>>();
int choiceID = 30;
lst.Add(from t in originalQuery
where t.tblProductChoiceTag.Any(c => c.ChoiceID == choiceID)
select t);
choiceID = 31;
lst.Add(from t in originalQuery
where t.tblProductChoiceTag.Any(c => c.ChoiceID == choiceID)
select t);
IQueryable<tblProduct> q = null;
bool first = true;
foreach (var tquery in lst)
{
if (first)
{
q = tquery;
first = false;
}
else
{ //the next one combine it
q = q.Union(tquery);
}
}
Do you know why this reference by val does the share choice ID in this query ?
Here is the code:
lst = new List<IQueryable<tblProduct>>();
int choiceID = 30;
lst.Add(from t in originalQuery
where t.tblProductChoiceTag.Any(c => c.ChoiceID == choiceID)
select t);
choiceID = 31;
lst.Add(from t in originalQuery
where t.tblProductChoiceTag.Any(c => c.ChoiceID == choiceID)
select t);
IQueryable<tblProduct> q = null;
bool first = true;
foreach (var tquery in lst)
{
if (first)
{
q = tquery;
first = false;
}
else
{ //the next one combine it
q = q.Union(tquery);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已捕获
choiceID
变量。请记住,(a) 查询执行被延迟,(b) 闭包捕获变量,而不是值。在您的情况下,您基本上希望为每个查询使用不同的变量,或者只是在变量代表的数字中进行编码。You've captured the
choiceID
variable. Remember that (a) query executions are deferred and (b) closures capture the variable, not the value. In your case, you'll basically want to use a different variable for each query or simply code in the number the variable is representing.