如何将这个内连接子查询从 SQL 重写为 Lambda
SELECT ulcch.ID, ulcch.UserLoginHistoryID, ulcch.StatusID,
ulcch.ClientModuleID, ulcch.DeviceState, ulcch.UpdatedAt, ulcch.CreatedAt
FROM UserLoginClientConnectionHistory AS ulcch INNER JOIN
(SELECT MAX(CreatedAt) AS maxCreatedAt
FROM UserLoginClientConnectionHistory AS ulcch1
GROUP BY UserLoginHistoryID) AS m ON m.maxCreatedAt = ulcch.CreatedAt
每天可能会有许多“设备状态”更新被审核到此登录表中。此查询返回每天的最后一个唯一的查询。
我希望将其重写为 Lambda 语句。这就是我已经走了多远,我不知道我是否走在正确的轨道上,并且我的 Max() 抛出了类型错误,可能是因为 group by 正在制作另一个列表或其他东西... 希望你能从我的对象示例中解决这个问题......:S
userLogin.UserLoginClientConnectionHistories.Where(x => x.CreatedAt ==
userLoginClientConnectionHistoryRepository.GetAll(
GenericStatus.Active).GroupBy(y => y.UserLoginHistoryID).Max(y => y.CreatedAt));
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我认为这符合您的要求:
这是一个测试平台:
输出:
I think this does what you want:
Here is a testbed for it:
Output:
这是作为 lambda 的内部连接部分。我假设 CreatedAt 是一个日期时间。
用户登录客户端连接历史记录
.GroupBy(ulcch1 =>;
新
{
名称 = ulcch1.名称
})
.选择(g =>
新
{
maxCreatedAt = (DateTime?)(g.Max (p => p.CreatedAt))
})
Here is the inner join portion as a lambda. I assumed CreatedAt was a dateTime.
UserLoginClientConnectionHistory
.GroupBy (ulcch1 =>
new
{
Name = ulcch1.Name
})
.Select (g =>
new
{
maxCreatedAt = (DateTime?)(g.Max (p => p.CreatedAt))
})
我认为您想按
CreatedAt
而不是UserLoginHistoryID
进行分组:这将返回共享最新
CreatedAt 的
UserLoginClientConnectionHistory
条目集值。I think you want to group by
CreatedAt
rather thanUserLoginHistoryID
:This will return the set of
UserLoginClientConnectionHistory
entries that share the most recentCreatedAt
value.感谢你们所有的帮助,我已经投票给你们了,但你们不会相信,但几个小时后我搜索了一个将 SQL 转换为 LINQ 的程序,令我惊讶的是发现了一个名为“Linqer"。听起来很疯狂,没想到会走得太远,但它工作得很好。如果其他人陷入同一条船上,绝对值得检查该应用程序...
检查它返回的庞大查询!分析完之后,不觉得多了额外的臃肿吗?有人有任何优化技巧或发现任何不必要的代码吗?
为你的帮助干杯 dahlbyk,但我确实想对 UserLoginHistoryID 进行分组,在深入研究 lambda 等效项之前,我已经在 SQL 中确认了我的查询:) 谢谢。
@Mark Thanks for taking the time to reply, yes I do what the [last] entries per user (userloginhistory.. which in turn contains a userID) for each day, and exporting my sql into the linq query did produce what I wanted (which can be seen in the query result below; this is what I want. The reason you see double entries for each day is because there are also attached ClientModule's.. so I really want all client module, per login entry per day - so hard to get a programming requirement across over a discussion forum argh!) Perhaps yours does exactly the same thing (it appears to if I am reading your output correctly) just a lot more streamlined.
我对您使用
GroupBy
和Select
所做的匿名转换不太了解,但现在我明白了,它是有道理的。我也许可以试试你的。希望我可以对其进行调整,以包含每天不同的 ClientModule。所以无论如何..这是我的 SQL 的查询结果,实际上是我通过自己的 lambda 得到的结果:Update Mark: Hi again, well after a couple of tweaks on your query, I could produce the same object graph in .NET between both lambda statements. This is the one I will use now, derived from yours as it's more streamlined and easier to understand than the auto-gen'd one and I will award you the points :)
我向 Group By 添加了更多条目,因为我的
new
ModuleDeviceState
类需要这些条目。Thanks for all your help guys, i've voted you all up, but you wouldn't believe it but a few hours later I searched for a program to convert SQL to LINQ, and to my surprise found one called "Linqer". Sounds crazy and didn't expect to get far, but it worked perfectly.. definitely worth checking out that app if anyone else gets stuck in the same boat...
Check the mammoth query it returned! After analysing it, don't think it's got extra bloat? Anyone have any optimisation tips or spot any unnecessary code?
Cheers for your help dahlbyk, but I did want to group on UserLoginHistoryID, I had my query confirmed in SQL before delving into a lambda equivalent :) thanks.
@Mark Thanks for taking the time to reply, yes I do what the [last] entries per user (userloginhistory.. which in turn contains a userID) for each day, and exporting my sql into the linq query did produce what I wanted (which can be seen in the query result below; this is what I want. The reason you see double entries for each day is because there are also attached ClientModule's.. so I really want all client module, per login entry per day - so hard to get a programming requirement across over a discussion forum argh!) Perhaps yours does exactly the same thing (it appears to if I am reading your output correctly) just a lot more streamlined.
See I didn't know too much about the anon casting you've done there with
GroupBy
andSelect
, but now I see it, it makes sense. I might give yours a go. Hopefully I can give it a tweak to include distinct ClientModule's per day too. So anyway.. here is the query result from my SQL, and effectively what I got through my own lambda:Update Mark: Hi again, well after a couple of tweaks on your query, I could produce the same object graph in .NET between both lambda statements. This is the one I will use now, derived from yours as it's more streamlined and easier to understand than the auto-gen'd one and I will award you the points :)
I added a few more entries to the Group By as I need that for my
new
ModuleDeviceState
class.