以下代码具有相同的结果,但以带来结果的速度更快

时间:2016-06-10 22:33:06

标签: c# linq-to-sql

第一个代码用于“加入

但是在第二个代码中没有使用“加入

请注意,结果相同。

所以我有几个问题

  • 哪个更好

  • 哪个更快

CODE01:

    (from Member in dContext.TB_FamilyCardMembers
    select new
    {
        Member.FamilyCard_ID,
        Member.TB_FamilyCard.NoFamilyCard,
        CardType = Member.TB_FamilyCard.Is_Card == true ? "دفتر عائلة" : "بيان عائلي",
        FirsN = Member.TB_Person.FirstName,
        FatherN = Member.TB_Person.FatherName == null ? SelectPersonByID(int.Parse(Member.TB_Person.Father_ID.ToString())).FirstName : Member.TB_Person.FatherName,
        LastN = Member.TB_Person.LastName == null ? SelectPersonByID(int.Parse(Member.TB_Person.Father_ID.ToString())).LastName : Member.TB_Person.LastName,
        MotherN = Member.TB_Person.MotherName == null ? SelectPersonByID(int.Parse(Member.TB_Person.Mother_ID.ToString())).FirstName : Member.TB_Person.MotherName,
        MotherLN = Member.TB_Person.MotherLastName == null ? SelectPersonByID(int.Parse(Member.TB_Person.Mother_ID.ToString())).LastName : Member.TB_Person.MotherLastName
    }).ToList();

______________________________________________

Code02:

(from Member in dContext.TB_FamilyCardMembers
join Card in dContext.TB_FamilyCards on Member.FamilyCard_ID equals Card.ID
join Person in dContext.TB_Persons on Member.Person_ID equals Person.ID
select new
{
    Member.FamilyCard_ID,
    Card.NoFamilyCard,
    CardType = Card.Is_Card == true ? "دفتر عائلة" : "بيان عائلي",
    FirsN = Person.FirstName,
    FatherN = Person.FatherName == null ? SelectPersonByID(int.Parse(Person.Father_ID.ToString())).FirstName : Person.FatherName,
    LastN = Person.LastName == null ? SelectPersonByID(int.Parse(Person.Father_ID.ToString())).LastName : Person.LastName,
    MotherN = Person.MotherName == null ? SelectPersonByID(int.Parse(Person.Mother_ID.ToString())).FirstName : Person.MotherName,
    MotherLN = Person.MotherLastName == null ? SelectPersonByID(int.Parse(Person.Mother_ID.ToString())).LastName : Person.MotherLastName
}).ToList();

1 个答案:

答案 0 :(得分:0)

  

所有道路都通往罗马

仅仅因为Linq代码中没有连接,并不意味着最终查询中没有连接。

使用后

Member.TB_Person.FirstName

Linq-2-sql可能会发布一个连接到生成的SQL。

许多编码器显式添加连接,因为它们以真实的SQL方式编写LINQ-2-SQL。大多数情况下它不需要(假设有适当的外键),因为L2S会为你计算出连接。

要真正回答您的问题,您需要分析实际生成的SQL。只有这样,您才会看到实际发送到数据库的查询的差异。机会是完全相同的。如果没有,请选择两者中效率最高的。

如何查看SQL: How to view LINQ Generated SQL statements?

相关问题