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

本文关键字:返回 Count 不能 | 更新日期: 2023-09-27 17:49:01

我有一个包含人员列表的表,其中有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
  • <
  • 状态/gh>
  • Mantelverzorgerid
  • Dokterid
  • 开斋节

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

看来这就是你的错误所在:

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

例如:

leeftijd1 = 0;
leeftijd2 = 150;

和评估:

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

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

你需要把这个改成:

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

在你的代码中有一个更明显的错误(我认为):if (leeftijd == "null")应该是if (string.IsNullOrWhiteSpace(leeftijd))