1

在下面的代码中,我希望 retrn n 行,但它总是返回零行,因为我的初始集有零记录。理想情况下,它应该执行 UNION ALL 并返回整数列表中所有整数的记录 clearTotals{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);
    }
4

2 回答 2

1

我认为问题在于您在查询中捕获了循环变量 ( c),因此在执行查询时,它仅使用 c 的最新值。尝试将 c 复制到循环内的变量中:

foreach (int c in clearanceTotals)
{
    int c2 = c;
    ...

        ...
        where a.Nationality == 15 && a.DoNotUse == false && a.ClearanceStatus == c2
        ...
于 2012-04-19T18:58:04.843 回答
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(); 
于 2012-04-20T13:17:40.927 回答