使用.Contains()和.ToLowerInvariant()的EF查询在字符串末尾找到匹配时没有匹配

时间:2021-06-14 20:55:03

Is it possible to step into a linq query? I have a linq to entity framework 4 query in it's simplest form:

是否可以进入linq查询?我有一个linq to entity framework 4查询,它是最简单的形式:

List = List.Where(f => f.Value.ToString().ToLowerInvariant().Contains(filter.ToLowerInvariant()));

It's a query against an Entity Framework DbContext and I'm having trouble seeing why it works for something like:

这是对实体框架DbContext的查询,我很难理解为什么它适用于以下内容:

List searching for 001 yields no results against the following list

搜索001的列表不会产生针对以下列表的结果

  1. Test001
  2. Test001
  3. Test002
  4. Test002
  5. Test003
  6. Test003
  7. Test004
  8. Test004

However any other search yields results (Such as t00 or Test)

然而,任何其他搜索产生结果(如t00或测试)

Update

Basically I'm looking for why a query such as the above wouldn't return a result when I'm using a contains and the value matches the end of a string vs just the middle or begining. It's really confusing.

基本上我正在寻找为什么当我使用contains时,如上所述的查询不会返回结果,并且值匹配字符串的结尾与中间或开头。这真的令人困惑。

OK, it appears to have something to do with ToLowerInvariant() - when I removed that method it works just fine.

好吧,它似乎与ToLowerInvariant()有关 - 当我删除该方法时,它工作得很好。

5 个解决方案

#1


21  

It appears that ToLowerInvariant() produces the error. ToLower() works just fine.

似乎ToLowerInvariant()产生错误。 ToLower()工作正常。

#2


3  

try working with linqpad

尝试使用linqpad

#3


0  

As Dani suggested, LinqPad is a great tool for this. And, even though it will not allow you to "step into" Linq queries, you can use an extended method offered in LinqPad named Dump() that will show you the value of IEnumerable or IQueryable objects as you progress through your query. It is extremely useful.

正如Dani所说,LinqPad是一个很好的工具。而且,即使它不允许您“进入”Linq查询,您也可以使用LinqPad中提供的名为Dump()的扩展方法,它将在您查询过程中向您显示IEnumerable或IQueryable对象的值。这非常有用。

#4


0  

Is this query being run against the database (LINQ to EF) or against objects already in memory (LINQ to objects). I'm guessing that it is LINQ to objects based on a comment you made on another answer, in which case you should be able to set a breakpoint in the lambda expression.

此查询是针对数据库(LINQ to EF)还是针对已存在于内存中的对象(LINQ to objects)运行的。我猜测它是基于你在另一个答案上做出的评论的对象的LINQ,在这种情况下你应该能够在lambda表达式中设置一个断点。

#5


0  

Why don't you do a selection on that ToLowerInvariant() to see what it is returning.

为什么不对ToLowerInvariant()进行选择以查看它返回的内容。

#1


21  

It appears that ToLowerInvariant() produces the error. ToLower() works just fine.

似乎ToLowerInvariant()产生错误。 ToLower()工作正常。

#2


3  

try working with linqpad

尝试使用linqpad

#3


0  

As Dani suggested, LinqPad is a great tool for this. And, even though it will not allow you to "step into" Linq queries, you can use an extended method offered in LinqPad named Dump() that will show you the value of IEnumerable or IQueryable objects as you progress through your query. It is extremely useful.

正如Dani所说,LinqPad是一个很好的工具。而且,即使它不允许您“进入”Linq查询,您也可以使用LinqPad中提供的名为Dump()的扩展方法,它将在您查询过程中向您显示IEnumerable或IQueryable对象的值。这非常有用。

#4


0  

Is this query being run against the database (LINQ to EF) or against objects already in memory (LINQ to objects). I'm guessing that it is LINQ to objects based on a comment you made on another answer, in which case you should be able to set a breakpoint in the lambda expression.

此查询是针对数据库(LINQ to EF)还是针对已存在于内存中的对象(LINQ to objects)运行的。我猜测它是基于你在另一个答案上做出的评论的对象的LINQ,在这种情况下你应该能够在lambda表达式中设置一个断点。

#5


0  

Why don't you do a selection on that ToLowerInvariant() to see what it is returning.

为什么不对ToLowerInvariant()进行选择以查看它返回的内容。