Simple sql to Linq query with group by and aggregate functions

后端 未结 4 1012
萌比男神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:49

    Since op mentioned learning syntax, here's a fluent version:

    company_user
        .Where(x => x.company_id == 1)
        .Join(
            user,
            x => x.user_id,
            x => x.user_id,
            (o,i) => new { 
                FirstName = i.firstName, 
                LastName = i.lastName,
                InvoiceCount = invoice.Count(y => y.user_id == o.user_id),
                InvoiceSum = invoice.Where(y => y.user_id == o.user_id).Sum(y => y.amount)
            }
        ).GroupBy(x => new { x.FirstName, x.LastName })
        .Distinct()
    

提交回复
热议问题