当前修订版的 LINQ 语法
如何使用 C# 和 LINQ 编写此 SQL 语句?我正在查询 Oracle 数据库,该表有多个记录修订版。因此,我只想要表中包含的每条记录的当前修订版本。
SQL 如下所示:
select TP_ID, TP_TEXT, TP_DEFN_SAKEY
from TP_DEFN tp1
where tp1.TP_ACTIVE_FLAG = 'Y' and
tp1.FAMILY_ID = 1 and
tp1.TP_DEFN_REV_DTS = (select max(TP_DEFN_REV_DTS)
from TP_DEFN tp2
where tp2.family_id = tp1.family_id and tp2.tp_id = tp1.tp_id )
order by TP_ID
TP_DEFN_REV_DTS
是存储当前修订版本的日期时间字段。
我是 LINQ 的初学者,一直在努力寻找可行的解决方案。每次我尝试在 LINQ 查询中进行分组时都会收到错误
不支持 GroupBy
How can I write this SQL statement using C# and LINQ? I am quering an Oracle database and the table has multiple revisions of the records. Therefore, I want onyl the current revision of each record contained in the table.
The SQL looks like this:
select TP_ID, TP_TEXT, TP_DEFN_SAKEY
from TP_DEFN tp1
where tp1.TP_ACTIVE_FLAG = 'Y' and
tp1.FAMILY_ID = 1 and
tp1.TP_DEFN_REV_DTS = (select max(TP_DEFN_REV_DTS)
from TP_DEFN tp2
where tp2.family_id = tp1.family_id and tp2.tp_id = tp1.tp_id )
order by TP_ID
TP_DEFN_REV_DTS
is the date time field that stores the current revision.
I am a beginner with LINQ and have been struggling to find an workable solution. Every time that I try grouping in the LINQ query I get an error
GroupBy is not supported
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试这样的事情:
Try something like this:
我突然想到,不知道您正在使用哪个 LINQ 提供程序...
Off the top of my head, and not knowing which LINQ provider you're using...
如果您使用实体框架或 linq-to-sql,则可以根据需要直接传递 sql(尽管这会阻止更改跟踪,至少默认情况下如此)。
对于 EF,请使用 ObjectContext.ExecuteStoreQuery: http://msdn.microsoft.com/en -us/library/dd487208.aspx
对于 L2S,使用 DataContext.ExecuteQuery:http://msdn.microsoft.com/en-us /library/system.data.linq.datacontext.executequery.aspx
If you're using entity framework or linq-to-sql, you can just pass the direct sql if you want (although that'll prevent change tracking, at least by default).
For EF, use ObjectContext.ExecuteStoreQuery: http://msdn.microsoft.com/en-us/library/dd487208.aspx
For L2S, use DataContext.ExecuteQuery: http://msdn.microsoft.com/en-us/library/system.data.linq.datacontext.executequery.aspx