LINQ with Group,Join and SQL Easy in SQL in not LINQ?

时间:2013-04-19 03:35:30

标签: performance linq entity-framework linq-to-sql

我无法理解如何将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 };

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

3 个答案:

答案 0 :(得分: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);
    }

必须提供以下结果:

<强> FirstCompany

<强> SecondCompany

答案 1 :(得分: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 };

答案 2 :(得分:0)

您按ss.City(供应商)进行分组。结果是IGrouping<City, Supplier>。即分组后只有CitySupplier才能到达:对于每个城市,您会获得IEnumerable<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,
                ...
            };