LINQ 左外连接语法差异

发布于 2024-11-25 09:48:58 字数 1569 浏览 1 评论 0原文

在 LINQ 中至少有两种执行 LEFT OUTER JOIN 的方法

class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

    static void Main()
    {
        // Example customers.
        var customers = new Customer[]
        {
            new Customer{ID = 5, Name = "Sam"},
            new Customer{ID = 6, Name = "Dave"},
            new Customer{ID = 7, Name = "Julia"},
            new Customer{ID = 8, Name = "Sue"},
            new Customer{ID = 9, Name = "Joe"}
        };

        // Example orders.
        var orders = new Order[]
        {
            new Order{ID = 5, Product = "Book"},
            new Order{ID = 6, Product = "Game"},
            new Order{ID = 7, Product = "Computer"},
            new Order{ID = 8, Product = "Shirt"},
            new Order{ID = 8, Product = "TShirt"}
        };


        // First Way
        var query = from c in customers
                join o in orders on c.ID equals o.ID into co
                from x in co.DefaultIfEmpty() 
                where x != null && x.Product == "Shirt"
                select new {c, x};

        // Second Way  
        var query2 = from c in customers
                     from o in orders
                     where c.ID == o.ID && o.Product == "Shirt"
                     select new {c, o};
    }

我发现了很多第一种方法的示例(使用“into”),这就是我用来执行 LEFT OUTER JOINS 的方法。最近发现了第二种方法,看起来更简单。我测试过,它们都产生相同的结果。现在我的问题是有任何隐藏的差异、性能,还是只是语法糖?

多谢。

There are at least two ways of doing LEFT OUTER JOIN in LINQ

class Customer
{
    public int ID { get; set; }
    public string Name { get; set; }
}

class Order
{
    public int ID { get; set; }
    public string Product { get; set; }
}

    static void Main()
    {
        // Example customers.
        var customers = new Customer[]
        {
            new Customer{ID = 5, Name = "Sam"},
            new Customer{ID = 6, Name = "Dave"},
            new Customer{ID = 7, Name = "Julia"},
            new Customer{ID = 8, Name = "Sue"},
            new Customer{ID = 9, Name = "Joe"}
        };

        // Example orders.
        var orders = new Order[]
        {
            new Order{ID = 5, Product = "Book"},
            new Order{ID = 6, Product = "Game"},
            new Order{ID = 7, Product = "Computer"},
            new Order{ID = 8, Product = "Shirt"},
            new Order{ID = 8, Product = "TShirt"}
        };


        // First Way
        var query = from c in customers
                join o in orders on c.ID equals o.ID into co
                from x in co.DefaultIfEmpty() 
                where x != null && x.Product == "Shirt"
                select new {c, x};

        // Second Way  
        var query2 = from c in customers
                     from o in orders
                     where c.ID == o.ID && o.Product == "Shirt"
                     select new {c, o};
    }

I found a lot of examples of First Way (using 'into'), and that is the way I used to do my LEFT OUTER JOINS. Recently I found the second way, and looks like it is simpler. I tested and both of them produce the same result. Now my question are there any hidden differences, performance, or it is only syntactic sugar?

Thanks a lot.

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

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

发布评论

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

评论(1

べ繥欢鉨o。 2024-12-02 09:48:58

如果不使用 !=null 检查,则应使用第一种方法

左外连接从左表中选择所有与右表中匹配的项目,如果右表中不存在匹配项,结果仍然是返回但右表中的值为 null(linq 将其转换为集合项类型的默认值)

您的两个查询都有效地执行了内部联接,因此它们将是相同的。

为了得到不同的结果

 // First Way
 var query = from c in customers
 join o in orders on c.ID equals o.ID into co
 from x in co.DefaultIfEmpty()
 where c.Id>5 
 select new {c, x};

 // Second Way
 var query2 = from c in customers
 from o in orders
 where c.ID == o.ID && c.ID > 5
 select new {c, o};

The First way should be used if you don't use the !=null check

A left outer join selects everything from the left table with all the items that match in the right table, if no matches exist in the right table the result is still returned but null for the values in the right table (linq translates this as the default value for the collection item type)

Both your queries effectively perform an inner join so they will be the same.

To get different results

 // First Way
 var query = from c in customers
 join o in orders on c.ID equals o.ID into co
 from x in co.DefaultIfEmpty()
 where c.Id>5 
 select new {c, x};

 // Second Way
 var query2 = from c in customers
 from o in orders
 where c.ID == o.ID && c.ID > 5
 select new {c, o};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文