限制 Linq to Castle ActiveRecord 生成的 Select 语句中的字段
我正在使用 ActiveRecord 和 LinqToActiveRecord 来查询我的数据库。问题是,当不需要时,从 LINQ 表达式生成的 SQL 语句总是尝试选择所有可能的字段,包括连接表。例如,我有这些数据库表:
Table: MasterTable
Id (int)
ChildId (int) <- references a record in the Child table
Table: ChildTable
Id (int)
ChildName (varchar)
现在我想查询引用的子记录包含 ChildName“Tijn”的所有主记录:
var myList = (from master in MasterTable.Queryable
where master.Child.ChildName == "Tijn"
select master).ToList();
生成的 SQL 语句不仅尝试选择所有主字段,还尝试选择连接的子字段!当我使用 HQL 尝试相同的操作时:
select master from MasterTable master where master.Child.ChildName = 'Tijn'
生成的 SQL 语句仅包含主字段,就像我想要的那样。
那么,简而言之:如何“限制”/“约束”在 LINQtoActiveRecord / LINQtoNHibernate 中选择哪些字段的表?某种“投影”或扩展的“选择”方法?
I'm using ActiveRecord and LinqToActiveRecord to query my database. The problem is, that the generated SQL statement from my LINQ expression always tries to select all possible fields, including joined tables, when this is not needed. For example, I have these database tables:
Table: MasterTable
Id (int)
ChildId (int) <- references a record in the Child table
Table: ChildTable
Id (int)
ChildName (varchar)
Now I want to query all Master records of which the referenced Child record contains ChildName "Tijn":
var myList = (from master in MasterTable.Queryable
where master.Child.ChildName == "Tijn"
select master).ToList();
The generated SQL statement tries to select not only all Master fields, but the joined Child fields too! When I try the same using HQL:
select master from MasterTable master where master.Child.ChildName = 'Tijn'
the generated SQL statement only includes the master fields, like I wanted to.
So, to be short: How can I 'limit' / 'constrain' the tables of which fields are selected in LINQtoActiveRecord / LINQtoNHibernate? Some kind of 'projection' or extended 'Select' method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论