Count不能正确返回正确的计数

时间:2015-05-15 17:27:40

标签: c# database linq asp.net-web-api

我有一张包含人员名单的表格,其中5人年龄介于12至25岁之间。这些人住在林堡,是男性。

string[] leeftijdstring = new string[2];
int leeftijd1;
int leeftijd2;
int getal = 0;
if (leeftijd == "null")
{
    leeftijd1 = 0;
    leeftijd2 = 150;
}
else
{
    leeftijdstring = leeftijd.Split('-');
    leeftijd1 = Convert.ToInt32(leeftijdstring[0]);
    leeftijd2 = Convert.ToInt32(leeftijdstring[1]);
}
var count = (from p in _db.Personen
    join pc in _db.Postcodes on p.Postcode equals pc.postcode
    join r in _db.Regios on pc.RegioId equals r.RegioId
    where (p.Leeftijd >= leeftijd1 && leeftijd2 <= p.Leeftijd) &&
    r.RegioNaam == regio && p.Geslacht == geslacht
    select p.PersoonId).Distinct().Count();

无论如何,count为我返回0,但至少应该有4个匹配搜索!

我的表格如下:

Personen:

  • 纳摩
  • Voornaam
  • Leeftijd
  • Geslacht
  • 标地址
  • 邮编:
    • 邮编
    • Gemeente
    • 雷吉奥:
      • Regioid
      • RegioNaam
    • PostcodeId
  • Telefoon
  • 电子邮件
  • Wachtwoord
  • Rollid
  • Vragenlijstid
  • 状态
  • Mantelverzorgerid
  • Dokterid
  • Eid的

1 个答案:

答案 0 :(得分:2)

这似乎是您的错误所在:

where (p.Leeftijd >= leeftijd1 && leeftijd2 <= p.Leeftijd)

例如,我们采取:

leeftijd1 = 0;
leeftijd2 = 150;

并评估:

where (p.Leeftijd >= 0 && 150 <= p.Leeftijd)    

如果true,则只返回p.Leeftijd >= 150

您需要将其更改为:

where (p.Leeftijd >= leeftijd1 && p.Leeftijd <= leeftijd2)

您的代码中还有一个明显的错误(我认为):if (leeftijd == "null")应为if (string.IsNullOrWhiteSpace(leeftijd))

相关问题