如何将此SQL查询转换为LINQ或Lambda表达式?

时间:2022-11-16 23:43:31

I have the following SQL query:

我有以下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'

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?

有了上述数据,是否可能将SQL查询转换为LINQ或Lambda表达式?

Any help is appreciated!

任何帮助都是赞赏!

2 个解决方案

#1


3  

This might do what you want:

这可能是你想要的:

Layout
    .Where(x => Position
        .Where(y => y.Code == "TopMenu")
        .Select(y => SqlClient.SqlMethods.Like(x.PositionID, "%" + y.ID.ToString() + "%")
        ).Count() > 0
    ).Join(
        Category,
        x => x.CategoryID,
        x => x.ID,
        (o,i) => new { ID = i.ID, Name = i.Name }
    )

Although you might want to materialize the 'Position' sub query to save on time like so:

虽然您可能希望实现“Position”子查询,以节省如下时间:

var innerSubQuery = Position.Where(y => y.Code == "TopMenu");

Layout
    .Where(x => innerSubQuery
        .Select(y => SqlClient.SqlMethods.Like(x.PositionID, "%" + y.ID.ToString() + "%")
        ).Count() > 0
    ).Join(
        Category,
        x => x.CategoryID,
        x => x.ID,
        (o,i) => new { ID = i.ID, Name = i.Name }
    );

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.

但是,我同意Jon的观点,要使您的生活更简单,您应该通过创建一个“Layout_Position”表来改变处理多对多关系的方式。

#2


2  

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:

你不能把第二个连接表示成一个连接,因为它不是一个等连接,但是这个应该可以做到:

from c in category
join l in layout on c.Id equals l.CategoryId
from p in position
where p.Id.Contains(l.PositionId)
select new { c.Id, c.Name };

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.)

注意,当你有超过9个位置时,你的“包含/类似”条款会给你带来不好的结果。有比使用逗号分隔列表更好的多对多关系方法。(如中间表)

#1


3  

This might do what you want:

这可能是你想要的:

Layout
    .Where(x => Position
        .Where(y => y.Code == "TopMenu")
        .Select(y => SqlClient.SqlMethods.Like(x.PositionID, "%" + y.ID.ToString() + "%")
        ).Count() > 0
    ).Join(
        Category,
        x => x.CategoryID,
        x => x.ID,
        (o,i) => new { ID = i.ID, Name = i.Name }
    )

Although you might want to materialize the 'Position' sub query to save on time like so:

虽然您可能希望实现“Position”子查询,以节省如下时间:

var innerSubQuery = Position.Where(y => y.Code == "TopMenu");

Layout
    .Where(x => innerSubQuery
        .Select(y => SqlClient.SqlMethods.Like(x.PositionID, "%" + y.ID.ToString() + "%")
        ).Count() > 0
    ).Join(
        Category,
        x => x.CategoryID,
        x => x.ID,
        (o,i) => new { ID = i.ID, Name = i.Name }
    );

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.

但是,我同意Jon的观点,要使您的生活更简单,您应该通过创建一个“Layout_Position”表来改变处理多对多关系的方式。

#2


2  

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:

你不能把第二个连接表示成一个连接,因为它不是一个等连接,但是这个应该可以做到:

from c in category
join l in layout on c.Id equals l.CategoryId
from p in position
where p.Id.Contains(l.PositionId)
select new { c.Id, c.Name };

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.)

注意,当你有超过9个位置时,你的“包含/类似”条款会给你带来不好的结果。有比使用逗号分隔列表更好的多对多关系方法。(如中间表)