如何将此 SQL 查询转换为 LINQ 或 Lambda 表达式?
我有以下 SQL 查询:
SELECT C.ID, C.Name FROM Category C JOIN Layout L ON C.ID = L.CategoryID
JOIN Position P ON L.PositionID LIKE '%' + CAST(P.ID AS VARCHAR) + '%'
WHERE P.Code = 'TopMenu'
和以下数据
位置:
ID Code
1 TopMenu
2 BottomMenu
类别
ID Name
1 Home
2 Contact
3 About
布局
ID CategoryID PositionID
1 1 1
2 2 1,2
3 3 1,2
有了上述数据,是否可以将 SQL 查询转换为 LINQ 或 Lambda 表达式?
任何帮助表示赞赏!
I have the following SQL query:
SELECT C.ID, C.Name FROM Category C JOIN Layout L ON C.ID = L.CategoryID
JOIN Position P ON L.PositionID LIKE '%' + CAST(P.ID AS VARCHAR) + '%'
WHERE P.Code = 'TopMenu'
and following data
Position:
ID Code
1 TopMenu
2 BottomMenu
Category
ID Name
1 Home
2 Contact
3 About
Layout
ID CategoryID PositionID
1 1 1
2 2 1,2
3 3 1,2
With the above data, is it possible to convert the SQL query to LINQ or Lambda expression?
Any help is appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这可能会满足您的要求:
尽管您可能希望实现“位置”子查询以节省时间,如下所示:
但是,我同意乔恩的观点,为了真正让您的生活更简单,您应该改变处理方式通过创建“Layout_Position”表来建立多对多关系。
This might do what you want:
Although you might want to materialize the 'Position' sub query to save on time like so:
I do, however, agree with Jon that to really make your life simpler you should change the way you're handling the many-to-many relationship by creating a 'Layout_Position' table.
好吧,您将无法将第二个连接表示为连接,因为它不是等值连接,但这应该可以做到:
请注意,当您有超过9 个职位。对于多对多关系,有比使用逗号分隔列表更好的方法。 (例如中间表。)
Well, you won't be able to express the second join as a join, because it's not an equijoin, but this should do it:
Note that your "contains/LIKE" clause will give you bad results when you've got more than 9 positions. There are better approaches to many-to-many relations than using a comma-separated list. (Such as an intermediate table.)