简体   繁体   English

NHibernate.LINQ支持的运算符

[英]NHibernate.LINQ Supported Operators

I'm trying to evaluate NHibernate.LINQ 1.0 without actually writing any code. 我正在尝试评估NHibernate.LINQ 1.0而不实际编写任何代码。 Ayende has admitted that this version of LINQ support is subpar compared to EF , but for the life of me I can't seem to find a page that explains what's supported and unsupported in this implementation. Ayende承认, 与EF相比这个版本的LINQ支持是次要的 ,但对于我的生活,我似乎无法找到一个页面来解释这个实现中支持和不支持的内容。 For example, can I use Skip & Take ? 例如,我可以使用SkipTake吗? What can I not use? 我不能用什么?

You can check LINQ for NHibernate examples to see tests done by Ayende himself on what's implemented and what's not for this provider. 您可以查看LINQ for NHibernate示例,以查看Ayende自己完成的测试,了解已实施的内容以及针对此提供商的内容。

Some of those generally supported: 其中一些人普遍支持:

  • Anonymous type creation. 匿名类型创建。 new { Person = x.Name }
  • First(). 第一()。 query.First()
  • FirstOrDefault(). FirstOrDefault()。 query.FirstOrDefault()
  • Single(). 单()。 query.Single()
  • SingleOrDefault(). 的SingleOrDefault()。 query.SingleOrDefault()
  • Aggregate(). 骨料()。 query.Aggregate((x1,x2) => x1)
  • Contains(). 载有()。 query.Where(x => x.Name.Contains("Foo"))
  • StartsWith(). 以。。开始()。
  • EndsWith(). 以。。结束()。
  • Substring(). 子串()。 where db.Methods.Substring(e.FirstName, 1, 2) == "An"
  • Sub-queries. 子查询。 query.Where(x => x.Company.Id == 4)
  • Count(). 计数()。 query.Where(x => x.Relatives.Count > 0)
  • Any(). 任何()。 query.Any()
  • Take(). 采取()。 query.Take(10)
  • Skip(). 跳跃()。 query.Take(10).Skip(4)
  • OrderBy(). 排序依据()。 orderby x.Name descending
  • Replace(). 更换()。 AfterMethod = e.FirstName.Replace("An", "Zan"),
  • CharIndex(). CHARINDEX()。 where db.Methods.CharIndex(e.FirstName, 'A') == 1
  • IndexOf(). 指数()。 where e.FirstName.IndexOf("An") == 1

Problematic: Group by Joins One of my own examples:

  • Problematic: Group by Joins One of my own examples:
  • Problematic: Group by Joins One of my own examples:

Problematic: Group by Joins One of my own examples:

 query = NSession.Session.Linq<Catalog>() .Where(acc => acc.Company.Status == "A") .Where(acc => acc.Id.StartsWith("12-536")) .Where(acc => acc.Id.EndsWith("92") || acc.Id.EndsWith("67")) .Take(10).OrderBy(acc => acc.Title); 

If you're production application is using the latest stable build 2.1.2.4 like I am, you're stuck with what the NHibernate.Linq provider gives us until NHibernate 3.0 (trunk) gets a stable release and we feel safe enough to use it on major applications. 如果你的生产应用程序正在使用最新的稳定版本2.1.2.4,就像我一样,你会遇到NHibernate.Linq提供商给我们的东西,直到NHibernate 3.0(trunk)得到一个稳定的版本,我们觉得安全到足以使用它主要应用。 Until then, I'm more than happy with a mixture of NHibernate.Linq and HQL. 在那之前,我对NHibernate.Linq和HQL的混合物感到非常满意。

The basic test of whether NHibernate can work with a Linq statement is whether you can serialize that statement's expression tree, then deserialize it in a different process and get the right answer. NHibernate是否可以使用Linq语句的基本测试是,您是否可以序列化该语句的表达式树,然后在不同的进程中反序列化它并获得正确的答案。 That means no external closures; 这意味着没有外部封闭; the lambda must work only with what it creates or is given as a parameter. lambda必须只与它创建的内容一起工作或作为参数给出。

Linq2NH 1.0, IIRC, also chokes when using members of the class that are not mapped, so if, for instance, you have a read-only calculated property like a special weighted or rolling average, you must map it to a DB column in order to reference it in the lambda (or recreate the logic in the lambda). 当使用未映射的类的成员时,Linq2NH 1.0,IIRC也会窒息,因此,例如,如果您具有特殊加权或滚动平均值等只读计算属性,则必须按顺序将其映射到DB列在lambda中引用它(或重新创建lambda中的逻辑)。 This is because the expression tree will eventually be boiled down into SQL (through one of NH's intermediates; in 2.x it's ICriteria, in 3.x it's HQL) and if NH cannot take an expression and convert it 1:1 into a SQL expression that will evaluate successfully, it's just not going to work. 这是因为表达式树最终将被归结为SQL(通过NH的中间体之一;在2.x中它是ICriteria,在3.x中它是HQL)并且如果NH不能采用表达式并将其1:1转换为SQL将成功评估的表达式,它不会起作用。

There is one special case: Linq2NH, IIRC, is smart enough to turn an IList.Contains() expression into an IN clause. 有一个特例:Linq2NH,IIRC,足够聪明,可以将IList.Contains()表达式转换为IN子句。 The list must be defined within the lambda (like new[]{"1","2"}.Contains(m.ID) ). 该列表必须在lambda中定义(如new[]{"1","2"}.Contains(m.ID) )。

The blog post from Ayende is from May this year. Ayende的博客文章是从今年5月开始的。 A lot of things changed. 很多事情发生了变化。 The NHiberante. NHiberante。 Linq 1.0 linq provider is deprecated since about a year because of the new linq provider in the NHibernate Trunk. 由于NHibernate Trunk中新的linq提供程序,Linq 1.0 linq提供程序大约一年后被弃用。 The new linq provider is not completely finished yet, but already very complete and usable for much more than the old linq provider. 新的linq提供程序尚未完全完成,但已经非常完整,可用于远旧的linq提供程序。 Things that do not work with the new linq provider are considered bugs and will be solved some day when reported. 对新的linq提供程序不起作用的东西被认为是错误,并且会在报告的某一天得到解决。

You can use skip and take with the old and new linq provider. 您可以使用skip和take旧的和新的linq提供程序。 The current list of known issues can be found on NHibernate Jira . 可以在NHibernate Jira上找到当前已知问题的列表。 Other issues are unknown and all other features are already supported. 其他问题未知,并且已支持所有其他功能。

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

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