如何使用常规 SQL 和 Linq-To-Entities 通过 1-2 次查询检索所有记录?

发布于 2024-11-05 15:48:59 字数 496 浏览 0 评论 0原文

我在 Picasa 上上传了简化的 SQL ERD。 我尝试这样做,但我通过两个以上的查询才成功。

我需要从表 D 中检索具有表 E 的特定 E_Id 和表 TypeId 的所有记录>A

更新:我还需要从 B 映射到 TypeId 的所有记录也将被检索,即使不是全部它们映射在表 C 中(某种左右连接)

我最多需要执行 2 个查询,然后将其转换为 Linq-To-Entites 语句 如果可以用 1 个查询完成,我更喜欢用 1 个查询,

谢谢

I uploaded simplified SQL ERD on Picasa.
I tried to do it but I manage to it with more than 2 queries.

I need to retrieve all records from table D with specific E_Id of table E and specific TypeId of table A

UPDATED: I need also that all records from B that mapped to TypeId will be alse retrieved even if not all of them are mapped in table C (some kind of LEFT-RIGHT JOIN)

I need to do it at most 2 queries and then convert it to Linq-To-Entites statement
If it can be done with 1 query I prefer with 1 query

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

下雨或天晴 2024-11-12 15:48:59

SQL 查询:

SELECT D.*
FROM D 
INNER JOIN C ON C.Id = D.C_Id
INNER JOIN B ON B.Id = C.B_Id
INNER JOIN A ON A.Id = B.A_Id
WHERE D.E_Id = @eId AND A.TypeId = @typeId

SELECT B.*
FROM B
INNER JOIN A ON A.Id = B.A_Id
WHERE A.TypeId = @typeId

直接 Linq(如果导航属性不存在):

var query = from d in context.D
            join c in context.C on c.Id equals d.cId
            join b in context.B on b.Id equals c.bId
            join a in context.A on a.Id equals b.aId
            where d.eId == eId && a.TypeId == typeId
            select d;

var query2 = from b in context.B
             join a in context.A on a.Id equals b.aId
             where a.TypeId == typeId
             select b;

如果导航属性设置正确,则使用 Linq:

var query = from d in context.D
            where d.E.Id == eId &&
                  d.C.B.A.TypeId == typeId
            select d;  

var query2 = from b in context.B
             where b.A.TypeId == typeId
             select b; 

SQL query:

SELECT D.*
FROM D 
INNER JOIN C ON C.Id = D.C_Id
INNER JOIN B ON B.Id = C.B_Id
INNER JOIN A ON A.Id = B.A_Id
WHERE D.E_Id = @eId AND A.TypeId = @typeId

SELECT B.*
FROM B
INNER JOIN A ON A.Id = B.A_Id
WHERE A.TypeId = @typeId

Linq directly (if navigation properties don't exists):

var query = from d in context.D
            join c in context.C on c.Id equals d.cId
            join b in context.B on b.Id equals c.bId
            join a in context.A on a.Id equals b.aId
            where d.eId == eId && a.TypeId == typeId
            select d;

var query2 = from b in context.B
             join a in context.A on a.Id equals b.aId
             where a.TypeId == typeId
             select b;

Linq if navigation properties are correctly set up:

var query = from d in context.D
            where d.E.Id == eId &&
                  d.C.B.A.TypeId == typeId
            select d;  

var query2 = from b in context.B
             where b.A.TypeId == typeId
             select b; 
不必在意 2024-11-12 15:48:59

以下内容对您不起作用有什么原因吗?

SELECT D.*
FROM D
INNER JOIN C
ON C.Id = D.C_Id
INNER JOIN B
ON B.Id = C.B_Id
INNER JOIN A
ON A.Id = B.A_Id
WHERE D.E_id = <The E_Id> AND A.Typeid = <The Typeid>

Any reason why the below won't work for you?

SELECT D.*
FROM D
INNER JOIN C
ON C.Id = D.C_Id
INNER JOIN B
ON B.Id = C.B_Id
INNER JOIN A
ON A.Id = B.A_Id
WHERE D.E_id = <The E_Id> AND A.Typeid = <The Typeid>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文