c#等效的VB LINQ查询

本文关键字:LINQ 查询 VB | 更新日期: 2023-09-27 18:02:50

我在VB中有以下查询,但我不知道如何将其转换为c#语法

 Dim q = From c In db.Customers
        Group Join o In db.Orders On c.CustomerID Equals o.CustomerID Into orders = Group 
        Select New With {c.ContactName, .OrderCount = orders.Count()}

谢谢

c#等效的VB LINQ查询

这其实很简单。你只需要去掉Group:

var q = from c in db.Customers
        join o in db.Orders on c.CustomerID equals o.CustomerID into orders
        select new { c.ContactName, OrderCount = orders.Count() };

或者,如果您正在寻找lambda语法:

var q = db.Customers.GroupJoin(db.Orders,
                               o => o.CustomerID,
                               c => c.CustomerID,
                               (c, orders) =>
                                   new
                                   {
                                       c.ContactName, 
                                       OrderCount = orders.Count()
                                   });
Dim q = From c In db.Customers
    Group Join o In db.Orders On c.CustomerID Equals o.CustomerID Into orders = Group 
    Select New With {c.ContactName, .OrderCount = orders.Count()}
var q =
    from c in db.Customers
    join o in db.Orders on c.CustomerID equals o.CustomerID into orders
    select new {c.ContactName, .OrderCount = orders.Count()};