在下面的代码中,我希望重新排列n行但它总是返回零行,因为我的初始集合没有记录。理想情况下,它应该是一个UNION ALL并返回整数列表中所有整数的记录clearanceTotals {6,7,8,9,17} 知道如何去做吗?
var tbl = (from a in db.Applicants
where a.Id == null
select new { a.Id, a.Firstname, a.Lastname });
int thisTag;
foreach (int c in clearanceTotals)
{
switch (c)
{
case 6:
thisTag = 38;
break;
case 8:
thisTag = 39;
break;
case 17:
thisTag = 39;
break;
case 7:
thisTag = 42;
break;
case 9:
thisTag = 44;
break;
}
tbl = (from a in db.Applicants
join ad in db.ApplicantDeployments on a.Id equals ad.ApplicantId
join aa in db.ApplicantAttachments on a.Id equals aa.ApplicantId
where a.Nationality == 15 && a.DoNotUse == false && a.ClearanceStatus == c
&& aa.Tag == thisTag
select new { a.Id, a.Firstname, a.Lastname }).Union(tbl);
}
答案 0 :(得分:1)
我认为问题在于您在查询中捕获循环变量(c
),因此在执行查询时,它仅使用最新的c值。尝试将c复制到循环内的变量:
foreach (int c in clearanceTotals)
{
int c2 = c;
...
...
where a.Nationality == 15 && a.DoNotUse == false && a.ClearanceStatus == c2
...
答案 1 :(得分:0)
好的,所以我让这个工作,但它的附加代码加上额外的变量
var temp = (from a in db.Applicants
where a.Id == null
select a.Id).ToList();
int thisTag;
foreach (int c in clearanceTotals)
{
switch (c)
{
case 6:
thisTag = 38;
break;
case 8:
thisTag = 39;
break;
case 17:
thisTag = 39;
break;
case 7:
thisTag = 42;
break;
case 9:
thisTag = 44;
break;
}
temp = temp.Union(from a in db.Applicants
join ad in db.ApplicantDeployments on a.Id equals ad.ApplicantId
join aa in db.ApplicantAttachments on a.Id equals aa.ApplicantId
where a.Nationality == 15 && a.DoNotUse == false && a.ClearanceStatus == c
&& aa.Tag == thisTag
select a.Id).ToList();
}
var tbl = (from a in db.Applicants
join ad in db.ApplicantDeployments on a.Id equals ad.ApplicantId
where temp.Contains(a.Id)
select new { a.Id, a.Firstname, a.Lastname }).Distinct();