NHibernate LINQ 在 Where() 子句中支持 ToLower() 吗? [英] Does NHibernate LINQ support ToLower() in Where() clauses?

查看:21
本文介绍了NHibernate LINQ 在 Where() 子句中支持 ToLower() 吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实体及其映射:

I have an entity and its mapping:

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);
    }
}

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

I'm trying to run a query on it (to grab it out of the database):

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

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

However, whenever I run this query, I get the following exception:

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.

Am I right when I say that, currently, Linq to NHibernate does not support ToLower()? And if so, is there an alternative that allows me to search for a string in the middle of another string that Linq to NHibernate is compatible with? For example, if the user searches for kap, I need it to match Kapiolani, Makapuu, and Lapkap.

推荐答案

我最近遇到了这种情况.我可以告诉您 ToLower() 不起作用,而 Contains() 和 StartsWith() 起作用并且不区分大小写.您可以直接使用 Contains() 和 StartsWith() 获得所需的效果.

I had this happen recently. I can tell you that ToLower() does not work and that Contains() and StartsWith() do work and are not case sensitive. You can get the desired affect by using Contains() and StartsWith() directly.

这篇关于NHibernate LINQ 在 Where() 子句中支持 ToLower() 吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆