[英]Can anybody tell me why this linq query won't work?
Sorry if I'm missing something really obvious, I've been coding for a lot of hours in a row and my brain is crawling to a halt. 抱歉,如果我缺少真正明显的东西,我已经连续进行了多个小时的编码,而我的大脑正停下来。 I have the following statement: 我有以下声明:
var hcn = "";
var forename = "";
var surname = "";
foreach (var casenoteResult in casenoteResults)
{
personResults.AddRange(_ctx.People.Where
(x => x.PAS_INT_NO == casenoteResult.PAS_INT_NO
&& x.CSA_NO.Contains(hcn)
&& x.FORENAMES.ToLower().Contains(forename.ToLower())
&& x.SURNAME.ToLower().Contains(surname.ToLower()))
.ToList());
}
And I get no result. 我没有结果。 The only thing I'm really looking for is the casenote. 我真正想要的唯一东西是案例注释。 Yet if I comment out each of the '&&'s, so I'm left with this: 但是,如果我注释掉每个“ &&”,那么我就剩下了:
foreach (var casenoteResult in casenoteResults)
{
personResults.AddRange(_ctx.People.Where
(x => x.PAS_INT_NO == casenoteResult.PAS_INT_NO)
.ToList());
}
I get 1 result, which is what I'm expected. 我得到1个结果,这是我所期望的。
Can anyone help me? 谁能帮我? Why does the first statement not return this 1 result? 为什么第一个语句不返回此1结果? Could it be that some of the fields that I'm comparing the empty strings to are null? 难道我正在比较空字符串的某些字段为null? The one record that gets found doesn't have any nulls in it. 找到的一条记录中没有任何空值。 I'm lost here. 我在这里迷路了。 Please help my poor battered brain! 请帮助我可怜的受虐大脑!
If I were you, I would re-write this code like below. 如果我是你,我将像下面这样重写此代码。 It's safer to build the queryable in parts to make sure you have a good handle on which values you are actually passing in to the query. 分部分地构建可查询的对象以确保您确实了解要实际传递给查询的值是比较安全的。 The reason why you are not getting any rows is probably because the query values going in to the query is not what you think they are or your database doesn't treat empty string as a wildcard. 之所以没有得到任何行,可能是因为查询中输入的查询值不是您认为的那样,或者数据库没有将空字符串视为通配符。 (Because based on what you posted, you are checking if a string contains an empty string which is always true in C# but may not be true for your database provider). (因为基于您发布的内容,所以您正在检查字符串是否包含空字符串,该空字符串在C#中始终为true,但对于您的数据库提供程序可能不是true)。
var queryable = _ctx.People.Where(w => caseNoteResults.Select(s => s.PAS_INT_NO).Contains(w.PAS_INT_NO));
queryable = string.IsNullOrEmpty(hcn) ? queryable : queryable.Where(w => w.CSA_NO.Contains(hcn, StringComparison.InvariantCulture));
queryable = string.IsNullOrEmpty(forename) ? queryable : queryable.Where(w => w.FORENAMES.Contains(forename, StringComparison.InvariantCultureIgnoreCase));
queryable = string.IsNullOrEmpty(surname) ? queryable : queryable.Where(w => w.SURNAME.Contains(surname, StringComparison.InvariantCultureIgnoreCase));
personResults.AddRange(queryable.ToList());
The idea is, if your hcn
, forename
and surname
are empty, no point in checking them. 这个想法是,如果您的hcn
,前forename
和surname
为空,则没有必要检查它们。
Also, make sure that you handle nulls safely if each of these fields are nullable. 另外,如果这些字段中的每个字段都可为空,请确保安全处理null。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.