1

我很难理解如何将 SQL 转换为 LINQ。我想做以下事情,但不知道如何让 Group By 工作

var query = from s in Supplier
            join o in Offers on s.Supp_ID equals o.Supp_ID
            join p in Product on o.Prod_ID equals p.Prod_ID
            where s.City == "Chicago"
            group s by s.City into Results
            select new { Name = Results.Name };

我只需要做一些简单的事情,比如显示这个简单查询的产品名称,group by 如何使用连接和 where?

4

3 回答 3

3

您还没有提供课程,所以我假设它们如下所示:

    public class Supplier
    {
        public int SupplierID { get; set; }

        public string SuppierName { get; set; }

        public string City { get; set; }
    }


    public class Product
    {
        public int ProductID { get; set; }

        public string ProductName { get; set; }
    }

    public class Offer
    {
        public int SupplierID { get; set; }

        public int ProductID { get; set; }
    }

然后我添加了测试数据:

    List<Supplier> supplierList = new List<Supplier>()
                                 {
                                     new Supplier() { SupplierID = 1, SuppierName = "FirstCompany", City = "Chicago"},
                                     new Supplier() { SupplierID = 2, SuppierName = "SecondCompany", City = "Chicago"},
                                     new Supplier() { SupplierID = 3, SuppierName = "ThirdCompany", City = "Chicago"},
                                 };


    List<Product> productList = new List<Product>()
                                {
                                    new Product() { ProductID = 1, ProductName = "FirstProduct" },
                                    new Product() { ProductID = 2, ProductName = "SecondProduct" },
                                    new Product() { ProductID = 3, ProductName = "ThirdProduct" }
                                };

    List<Offer> offerList = new List<Offer>()
                             {
                                 new Offer() { SupplierID = 1, ProductID = 2},
                                 new Offer() { SupplierID = 2, ProductID = 1},
                                 new Offer() { SupplierID = 2, ProductID = 3}
                             };

如果您想显示提供了哪些产品的供应商的名称,那么您的 LINQ 查询应该是这样的:

    IEnumerable<string> result = from supplier in supplierList
                                 join offer in offerList on supplier.SupplierID equals offer.SupplierID
                                 join product in productList on offer.ProductID equals product.ProductID
                                 where supplier.City == "Chicago"
                                 group supplier by supplier.SuppierName into g
                                 select g.Key;

您可以查看是否选择了正确的名称:

    foreach (string supplierName in result)
    {
        Console.WriteLine(supplierName);
    }

它必须给出以下结果:

第一公司

第二公司

于 2013-04-19T05:43:34.393 回答
0

你可以试试这个:

   var query = from s in Supplier
                    join o in Offers on s.Supp_ID equals o.Supp_ID
                    join p in Product on o.Prod_ID equals p.Prod_ID
                    where s.City == "Chicago"
                    group s 
                    by new {s.City, s.Name} //added this
                    into Results
                    select new { Name = Results.Key.Name };
于 2013-04-19T05:55:43.123 回答
0

您将s(供应商)按分组s.City。这样做的结果是IGrouping<City, Supplier>. 即仅在分组后触手可及:对于每个城市,您都会获得一个City供应商(顺便说一句,这将乘以连接数)。SupplierIEnumerable<Supplier>

由于您也有where s.City == "Chicago"按城市分组的条件是没有用的。只有一个城市。所以我认为你也可以这样做:

from s in Supplier
join o in Offers on s.Supp_ID equals o.Supp_ID
join p in Product on o.Prod_ID equals p.Prod_ID
where s.City == "Chicago"
select new { 
                City = s.City.Name, 
                Supplier = s.Name,
                Product = p.Name,
                ...
            };
于 2013-04-19T20:01:11.213 回答