Group Multiple Tables in LINQ

后端 未结 2 585
失恋的感觉
失恋的感觉 2020-12-24 10:57

I have a very simple SQL query:

SELECT r.SpaceID, Count (*), SpaceCode 
FROM Rider r JOIN Spaces s 
ON r.SpaceID = s.SpaceID
GROUP BY r.SpaceID, s.SpaceCode          


        
相关标签:
2条回答
  • 2020-12-24 11:40

    For grouping multiple tables you can do as:

    group new { r,s } by new { r.SpaceID, s.SpaceCode }
    
    0 讨论(0)
  • 2020-12-24 11:57

    this might help:

    (
        from r in db.Rider
        join s in db.Spaces
            on r.SpaceID equals s.SpaceID
        group new { r,s } by new { r.SpaceID, s.SpaceCode }
        into grp
        select new
        {
            Count=grp.Count(),
            grp.Key.SpaceID,
            grp.Key.SpaceCode
        }
    )
    

    Where db is the database context

    0 讨论(0)
提交回复
热议问题