Simple sql to Linq query with group by and aggregate functions

后端 未结 4 1008
萌比男神i
萌比男神i 2021-02-02 01:51

I\'m fighting with linq trying to learn the syntax and I can\'t figure out how to do the following simple query

SELECT DISTINCT
    user.firstname,
    user.last         


        
4条回答
  •  余生分开走
    2021-02-02 02:33

    The query you're after should be pretty close to this:

    var query =
        from cu in company_user
        where cu.company_id == 1
        join u in user on cu.user_id equals u.user_id
        join i in invoice on u.user_id equals i.user_id
        group i.amount by new
        {
            u.firstname,
            u.lastname,
        } into gs
        select new
        {
            firstname = gs.Key.firstname,
            lastname = gs.Key.lastname,
            count = gs.Count(),
            sum = gs.Sum(),
        };
    

    Enjoy!

提交回复
热议问题