linq(到 nHibernate):“就像”操作员

发布于 2024-10-28 05:55:02 字数 520 浏览 0 评论 0原文


给定一个字符串列表,我想检索名称包含给定字符串之一的所有项目。
例如 - 给定 {"foo", "kuku"} 我想检索员工 "Corfoo"、"kuku maluku" 和 "kukufoo"。
我已经尝试了以下操作,但出现了空引用异常(?)

query.Where(u => values.Any(v=> u.FullName.Contains(v)) );

以下生成了“Lambda 表达式不在范围内”异常。

query.Where(u => (values.Count(v => u.FullName.Contains(v)) > 0) );

知道如何做到这一点吗?
我正在考虑迭代值集合并为每个元素添加新条件。
问题是 - .Where() 函数是一个连词 (AND),我需要析取 (OR)...
(我正在使用带有 Linq 提供程序的 nH 2.1.2;尚未在 nH3.0 上尝试过此操作...)

Hi
Given a list of strings I want to retrieve all items whose names contain one of the given strings.
for example- given {"foo", "kuku"} I want to retrieve the employees "Corfoo", "kuku maluku" and "kukufoo".
I've tried the following, but got a null-reference exception(?)

query.Where(u => values.Any(v=> u.FullName.Contains(v)) );

The following produced a 'Lambda expression not in scope' exception.

query.Where(u => (values.Count(v => u.FullName.Contains(v)) > 0) );

any idea how this can be done?
I was thinking along the lines of iterating over the values collection and adding a new condition for each element.
problem is- the .Where() function is a conjunction (AND) and I need disjunction (OR)...
(I'm using nH 2.1.2 with Linq provider; hadn't tried this on nH3.0 yet...)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亣腦蒛氧 2024-11-04 05:55:02

如果您不仅限于 Linq 提供程序,而且还对 ICriteria API 开放,我建议使用以下内容:

List<string> fullnames = new List<string>() { "foo", "kuku" };
// prepare Query
var query = session.CreateCriteria(typeof(Employee));
// dynamically add Like-conditions combined with OR
Disjunction namesCriteria = Restrictions.Disjunction();
foreach (var name in fullnames)
{
    namesCriteria.Add(Restrictions.Like("FullName", name, MatchMode.Anywhere));
}
// add complete Disjunction to prepared query
query.Add(namesCriteria);
IList<Employee> list = query.List<Employee>();

我认为在 NHibernate.Linq 中尝试这一点即使不是不可能,也可能会更困难。使用 NH 3.0,您可以使用 QueryOver,这将摆脱魔术字符串。

If you are not limited to the Linq provider but also open to the ICriteria API, I suggest using the following:

List<string> fullnames = new List<string>() { "foo", "kuku" };
// prepare Query
var query = session.CreateCriteria(typeof(Employee));
// dynamically add Like-conditions combined with OR
Disjunction namesCriteria = Restrictions.Disjunction();
foreach (var name in fullnames)
{
    namesCriteria.Add(Restrictions.Like("FullName", name, MatchMode.Anywhere));
}
// add complete Disjunction to prepared query
query.Add(namesCriteria);
IList<Employee> list = query.List<Employee>();

I think trying that in NHibernate.Linq might be harder if not impossible. With NH 3.0 you could use QueryOver, which would get rid of the magic strings.

凉风有信 2024-11-04 05:55:02

我使用了以下代码希望有帮助;

 public IList; GetCitiesLike(字符串文本)
    {
        AutoCompleteDto autoCompleteDto = null;

        var cityList = UnitOfWork.CurrentSession.QueryOver()
            .Where(x => x.CityName.IsLike(text, MatchMode.Start))
            .SelectList(u => u
                                 .Select(x => x.Id).WithAlias(() => autoCompleteDto.Id)
                                 .Select(x => x.CityName).WithAlias(() => autoCompleteDto.Name)
                                 .Select(x => x.CityName).WithAlias(() => autoCompleteDto.Value))
            .TransformUsing(Transformers.AliasToBean())
            .List();


        返回城市列表;
    }

I Used the following code hope it helps;

   public IList<AutoCompleteDto> GetCitiesLike(string text)
    {
        AutoCompleteDto autoCompleteDto = null;

        var cityList = UnitOfWork.CurrentSession.QueryOver<City>()
            .Where(x => x.CityName.IsLike(text, MatchMode.Start))
            .SelectList(u => u
                                 .Select(x => x.Id).WithAlias(() => autoCompleteDto.Id)
                                 .Select(x => x.CityName).WithAlias(() => autoCompleteDto.Name)
                                 .Select(x => x.CityName).WithAlias(() => autoCompleteDto.Value))
            .TransformUsing(Transformers.AliasToBean<AutoCompleteDto>())
            .List<AutoCompleteDto>();


        return cityList;
    }
|煩躁 2024-11-04 05:55:02

我使用了以下编码风格

QueryOver

IQueryOver<Patient> rowCount = Session.QueryOver<Patient>().ToRowCountQuery();

                    IQueryOver<Patient> result = this.Session.QueryOver<Patient>()
                     .Where(p => (p.FullNameEn.IsLike("%" + criteria.Keyword.Replace(" ", "%") + "%"))
                         || (p.FullNameAr.IsLike("%" + criteria.Keyword.Replace(" ", "%") + "%"))
                         || (p.IdentityNO == criteria.Keyword)
                         || (p.MobileNO == criteria.Keyword)
                         || (p.PatientID == patientIDKeyword)
                      )
                      .OrderBy(p => p.FullNameEn).Asc
                      .Take(criteria.PageSize)
                      .Skip((criteria.Page - 1) * criteria.PageSize);


                    totalCount = result.ToRowCountQuery().FutureValue<int>().Value;
                    transaction.Commit();
                    return result.Future<Patient>().ToList();

LINQ

var query = this.LINQ;
query = query.Where(p => p.VendorNameAr.Contains(criteria.Keyword.Replace(" ", "%")) ||
                                             p.VendorNameEN.Contains(criteria.Keyword.Replace(" ", "%")));
return query.ToList();

i used the following coding styles

QueryOver

IQueryOver<Patient> rowCount = Session.QueryOver<Patient>().ToRowCountQuery();

                    IQueryOver<Patient> result = this.Session.QueryOver<Patient>()
                     .Where(p => (p.FullNameEn.IsLike("%" + criteria.Keyword.Replace(" ", "%") + "%"))
                         || (p.FullNameAr.IsLike("%" + criteria.Keyword.Replace(" ", "%") + "%"))
                         || (p.IdentityNO == criteria.Keyword)
                         || (p.MobileNO == criteria.Keyword)
                         || (p.PatientID == patientIDKeyword)
                      )
                      .OrderBy(p => p.FullNameEn).Asc
                      .Take(criteria.PageSize)
                      .Skip((criteria.Page - 1) * criteria.PageSize);


                    totalCount = result.ToRowCountQuery().FutureValue<int>().Value;
                    transaction.Commit();
                    return result.Future<Patient>().ToList();

LINQ

var query = this.LINQ;
query = query.Where(p => p.VendorNameAr.Contains(criteria.Keyword.Replace(" ", "%")) ||
                                             p.VendorNameEN.Contains(criteria.Keyword.Replace(" ", "%")));
return query.ToList();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文