LINQ多个连接与组不工作

时间:2018-03-22 08:36:06

标签: c# linq

经过多次努力后,我无法将其发挥作用 我有以下查询:

var query =  from citiez in db.cities 
             join site in db.sites on citiez.city_id equals site.city_id 
             join ords in db.orders on site.site_id equals ords.site_id 
             group site by site.site_id into grouped 
             select new { 
                 sit = grouped.Count(), 
                 cits = grouped.FirstOrDefault().orders
                       .Where(o => o.site.city.city_name == city) 
             }; 
var list = query.ToList();

它工作正常,但给出循环引用错误。 我已经搜索了它,但无法在我的情况下使用

我在 SQL 中尝试做的是:

SELECT s.site_id, COUNT(o.order_id) TotalOrders  
FROM city c  
INNER JOIN site s ON c.city_id = s.city_id  
INNER JOIN dbo.[order] o ON s.site_id = o.site_id  
WHERE c.city_id = 4  
GROUP BY s.site_id

Query在SSMS中返回所需的结果。

修改
这是我的Controller Action代码:

public ActionResult draw_chart(string city)
        {
            var query = from citiez in db.cities
                        join site in db.sites on citiez.city_id equals site.city_id
                        join ords in db.orders on site.site_id equals ords.site_id
                        group site by site.site_id into grouped
                        select new
                        {
                            sit = grouped.Count(),
                            cits = grouped.FirstOrDefault().orders
                                  .Where(o => o.site.city.city_name == city)
                        };
            var list = query.ToList();
            return Json(list, JsonRequestBehavior.AllowGet);
        }  

任何帮助都会得到很多赞赏。

3 个答案:

答案 0 :(得分:0)

Linq看起来不像你展示的SQL,大多数时候你不需要使用JOIN(前提是你有一个很好的数据库设计和关系设置)。根据您的SQL,您可以使用如下链接查询:

var result = db.Orders
    .Where(o => o.City_id == 4)
    .GroupBy(o => o.Site.Site_id)
    .Select(g => new { 
         Site_id = g.Key, 
         TotalOrders = g.Count 
       });

答案 1 :(得分:0)

您打算在此查询中表达什么:

var query =  from citiez in db.cities
             where citiez.city == city
             from site in citiez.sites
             select new {
                 sit = site.orders.count,
                 cits = site.orders
             };

这会给你每个网站的订单。

答案 2 :(得分:0)

尝试这个控制器动作。您将获得网站ID和网站订单

public ActionResult draw_chart(string city)
                {
                    var query = from citiez in db.cities
                                join site in db.sites on citiez.city_id equals site.city_id
                                join ords in db.orders on site.site_id equals ords.site_id
                                where  citiez.city_name == city                            
                                group site by site.site_id into grouped
                                select new
                                {

                                    siteId = grouped.Key,
                                    ordersforsite = grouped.Count(),
                                };
                    var list = query.ToList();
                    return Json(list, JsonRequestBehavior.AllowGet);
                }