14

我有一个实体及其映射:

public class Test
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string Description { get; set; }
}

public class TestMap : EntityMap<Test>
{
    public TestMap()
    {
        Id(x => x.Id);
        Map(x => x.Name);
        Map(x => x.Description);
    }
}

我正在尝试对其运行查询(将其从数据库中获取):

var keyword = "test" // this is coming in from the user
keyword = keyword.ToLower(); // convert it to all lower-case

var results = session.Linq<Test>
    .Where(x => x.Name.ToLower().Contains(keyword));

results.Count(); // execute the query

但是,每当我运行此查询时,都会出现以下异常:

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

我说的对吗,目前,Linq to NHibernate 不支持ToLower()?如果是这样,是否有替代方法允许我在 Linq to NHibernate 兼容的另一个字符串中间搜索一个字符串?例如,如果用户搜索kap,我需要它来匹配KapiolaniMakapuuLapkap

4

5 回答 5

11

围绕这个主题似乎有很多困惑。

  • “旧”Linq 提供程序(用于 NHibernate 2.x)可能不支持这一点。如果是这样的话,它永远不会,因为它不再被维护了。
  • 新的提供程序(包含在 NHibernate 3.x 中)确实支持它(尽管 ToUpper 和 ToLower 似乎是倒置的,请参阅http://groups.google.com/group/nhibernate-development/browse_thread/thread/a167216e466b3241
  • Contains and StartsWith map to the LIKE operator in SQL. They are not case insensitive themselves; it's the collation that makes them case insensitive, so that depends on how your column/schema were created.

Update (2010-04-09): bug confirmed and patch submitted, see https://nhibernate.jira.com/browse/NH-2169

Update (2010-05-21): patch was applied on 2010-05-01 and works as expected now.

于 2010-04-08T14:22:30.943 回答
10

我最近发生了这种情况。我可以告诉你 ToLower() 不起作用, Contains() 和 StartsWith() 起作用并且不区分大小写。您可以通过直接使用 Contains() 和 StartsWith() 来获得所需的效果。

于 2010-04-08T11:58:04.093 回答
1

根据这两个博客文章中的评论,此功能尚未实现

于 2010-04-08T07:47:01.013 回答
0

您可能想要确认数据库是否使用区分大小写。

如果没有,那么您不需要 .ToLower()

于 2010-04-08T11:53:42.597 回答
0

The accepted answer mentions using Contains() and StartsWith() which are good. but wouldn't work in cases when you want to be sure both strings are the same.

Using "==" will suffice since it is also case-insensitive. So, you no longer need to use ToLower() nor ToUpper();

于 2019-08-28T10:08:36.697 回答