简体   繁体   English

谁能告诉我为什么这个linq查询不起作用?

[英]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 ,前forenamesurname为空,则没有必要检查它们。

Also, make sure that you handle nulls safely if each of these fields are nullable. 另外,如果这些字段中的每个字段都可为空,请确保安全处理null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Take()在此Linq to Sql查询中不起作用 - Why won't the Take() work in this Linq to Sql query 任何人都可以帮忙告诉我为什么我的MergeSort不会排序列表? - Can anyone help tell me why my MergeSort won't sort a list? 谁能告诉我以下正则表达式为何与“ 2015”不匹配? - Can anyone tell me why the following regex won't match to “2015”? 请任何人都可以告诉我我们如何通过平面查询过滤基于单个日期的数据 - Please anybody could tell me that how we can filter the data on the base of single date by a plane query 有人可以告诉我为什么这简单的C#代码不起作用,涉及从自身内部递归调用方法以获得根类别ID。 - Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID 简单的LINQ to XML查询将不起作用 - simple LINQ to XML query won't work 为什么这个LINQ join语句不起作用? - Why won't this LINQ join statement work? LINQ to SQL建议-为什么不起作用? - LINQ to SQL advice - why won't it work? 为什么 WebProxy BypassProxyOnLocal 对我不起作用? - why won't WebProxy BypassProxyOnLocal work for me? 我怎么知道为什么/如何一些LINQ查询相互协作而另一些不相同? - How can I tell why/how some LINQ queries work with each other and others don't?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM