我正在尝试对联系人列表运行查询,并使用给定的字符串过滤器对其进行过滤。代码如下:
var query = Session.QueryOver<CrmContact>()
.Where(x => x.Account.ID == account.ID && x.IsActive && x.IsContact);
query.And(Restrictions.Disjunction()
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.FirstName), filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.LastName), filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Email), filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Company), filtString))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Phone.Substring(0, 3)), filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Phone.Substring(3, 3)), filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Phone.Substring(6, 4)), filtString, MatchMode.Anywhere)));
但是,我遇到了最后3个语句的问题,因为我在运行它时遇到“找不到方法子字符串”错误。我使用子字符串来尝试匹配电话号码的单独部分,如果过滤器是电话号码/一部分(例如“123”匹配“1234567890”而不是“0123456789”,因为这将跨越两个份)。
对此表示感谢,谢谢。
答案 0 :(得分:0)
在lambda中使用SubString
是linq-to-nhibernate的正确,而不是queryover。您正在使用queryover,请避免使用linq-to-nhibernate语法或使用Linq而不是QueryOver。
Linq(Contains
和==
不区分大小写取决于您的数据库,如果您的数据库已经不区分大小写,则可以删除ToUpper
次调用):
using System.Linq;
using NHibernate.Linq;
// ...
filtString = filtString.ToUpper();
var query = Session.Query<CrmContact>()
.Where(x => x.Account.ID == account.ID && x.IsActive && x.IsContact)
.Where(c => c.FirstName.ToUpper().Contains(filtString) ||
c.LastName.ToUpper().Contains(filtString) ||
c.Email.ToUpper().Contains(filtString) ||
c.Company.ToUpper() == filtString ||
c.Phone.ToUpper().Substring(0, 3).Contains(filtString) ||
c.Phone.ToUpper().Substring(3, 3).Contains(filtString) ||
c.Phone.ToUpper().Substring(6, 4).Contains(filtString));
QueryOver:
var query = Session.QueryOver<CrmContact>()
.Where(x => x.Account.ID == account.ID && x.IsActive && x.IsContact);
query.And(Restrictions.Disjunction()
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.FirstName),
filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.LastName),
filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Email),
filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(Projections.Property<CrmContact>(c => c.Company),
filtString))
.Add(Restrictions.InsensitiveLike(
Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property<DimServicePoint>(c => c.Phone),
Projections.Constant(0), Projections.Constant(3)),
filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(
Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property<DimServicePoint>(c => c.Phone),
Projections.Constant(3), Projections.Constant(3)),
filtString, MatchMode.Anywhere))
.Add(Restrictions.InsensitiveLike(
Projections.SqlFunction("substring", NHibernateUtil.String,
Projections.Property<DimServicePoint>(c => c.Phone),
Projections.Constant(6), Projections.Constant(4)),
filtString, MatchMode.Anywhere)));