简体   繁体   English

NHibernate Linq喜欢按顺序排序

[英]NHibernate Linq Like Order By Likeness

How can a NHibernate query be written to order by likeness? NHibernate查询如何按相似性顺序编写?

From this example 从这个例子

session.Linq<Theater>()
.Where(x => x.Name.StartsWith("test") && x.Name.EndsWith("test");

Source: NHibernate.Linq LIKE 资料来源: NHibernate.Linq LIKE

How could the order of the results returned be ordered by 'Likeness'? 返回的结果的顺序如何由“喜欢”排序?

ie

With a result set of 结果集

  • John Doe 约翰·杜
  • Jane Doel 简·多尔
  • Janice Mander 珍妮丝·曼德(Janice Mander)

If I were to search for 'Doe' I would get the results of (in this order) 如果我搜索“ Doe”,我将得到(按此顺序)的结果

  • John Doe 约翰·杜
  • Jane Doel 简·多尔

Thanks in advance. 提前致谢。

As far as I know, the Where clause occurs first, and the Order By is executed on the result set, so in order to list the items by their "likeness" you would need to do some fancy sequence checking in an expanded query format (not LINQ) or in C# natively after you get the result set by re-ordering it yourself. 据我所知,Where子句首先出现,并且Order By在结果集上执行,因此,为了按项目的“相似度”列出项目,您需要以扩展的查询格式进行一些复杂的顺序检查(而不是LINQ)或在C#中通过您自己重新排序来获得结果集。

It all depends on what your definition of likeness is. 这完全取决于您对相似性的定义。 By default ascending or descending is how the string is listed in a dictionary. 默认情况下,升序或降序是在字典中列出字符串的方式。 If you wanted to base it on the index of where the LIKE '%word%' occurs, then you still need to specify that criteria in the Order By statement separately. 如果要基于LIKE'%word%'的出现位置的索引,则仍需要在Order By语句中单独指定该条件。

You could use substrings and drop them into cases and order by those cases. 您可以使用子字符串,并将其放入案例并按案例排序。

order by case
when SUBSTRING(field,1,Len(field)) LIKE 'word%' then 1 
when SUBSTRING(field,2,Len(field)) LIKE 'word%' then 2
when SUBSTRING(field,3,Len(field)) LIKE 'word%' then 3
...
end

See here: Mysql: Order by like? 参见此处: Mysql:按喜欢排序?

and here: Find all strings that share at least X characters, order by likeness 在这里: 找到所有至少共享X个字符的字符串(按相似度排序)

EDIT: Create Criteria might be helpful for making this work in LINQ, but its easier to use expanded syntax IMO. 编辑:创建条件对于在LINQ中进行此工作可能会有所帮助,但它更易于使用扩展语法IMO。 nhibernate CreateCriteria wildcard Like when nhibernate CreateCriteria通配符就像什么时候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM