Linq To SQL 选择父行和最上面的子表行
SQL 中有相关示例,我理解它们,但我似乎无法理解 Linq-SQL 中的情况。
有两个表,“Accounts”和“AccountTransactions”,通过 Accounts 中的主键(令人惊讶的是 AccountID)相关联。
我需要选择每个帐户行和 1 个最近的(按降序排列时为前 1 个)子 AccountTransaction。
我尝试破解我发现的一些示例,但没有运气(我猜只是没有得到它)...
任何帮助表示赞赏...
There are examples around for this in SQL and I understand them, but I can't seem to wrap my head around it in Linq-SQL.
There are two tables, 'Accounts' and 'AccountTransactions' related by the primary key in Accounts, which is, surprise, AccountID.
I need to select each Account row and the 1 Most Recent (Top 1 when ordered Descending) child AccountTransaction.
I've tried to hack at some examples I've found but no luck (just not getting it I guess)...
Any help appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是一种可能的解决方案;它不太漂亮,但应该可以工作:
这将选择所有帐户,并且如果 AccountTransactions 表将 AccountID 作为外键,则将自动执行联接。您所需要做的就是获取帐户所需的详细信息并获取按日期排序的最新交易。
This is one possible solution; it's not pretty, but it should work:
This will select all accounts and, if the AccountTransactions table has AccountID as a foreign key, the join will be automatically performed. All you need to do is get the details you need for the account and get the latest transaction, ordered by date.
其中 c.LinkId == 3 && c.RegionId == 5
选择c.tblLink).FirstOrDefault();
在我的情况下工作正常......
希望它对其他人有帮助。
where c.LinkId == 3 && c.RegionId == 5
select c.tblLink).FirstOrDefault();
Working fine in my case .....
Hope it will help someone else.