English 中文(简体)
带有group-by和aggregate函数的简单sql到Linq查询
原标题:Simple sql to Linq query with group by and aggregate functions

我正在与linq进行斗争,试图学习语法,但我不知道如何进行以下简单的查询

SELECT DISTINCT
    user.firstname,
    user.lastname,
    COUNT(invoice.amount),
    SUM(invoice.amount)
FROM
    company_user
    INNER JOIN 
        user
    ON 
        company_user.user_id = user.user_id
    INNER JOIN 
        invoice
    ON
        invoice.user_id= invoice.user_id
WHERE 
    company_user.company_id = 1
GROUP BY
    user.firstname,
    user.lastname,
GO

任何帮助将其转化为linq都将是非常棒的。

最佳回答

您要查询的查询应该非常接近以下内容:

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(),
    };

享受

问题回答

既然op提到了学习语法,这里有一个流利的版本:

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()

嘿,这个网站可能会帮助你:

101林克样品

有大多数linq函数的例子,如果你不明白,如果有人不明白,我可以稍后为你写信。

我认为您的join子句中存在差异——您在user_id上的内部join发票本身。我想你打算加入这个用户?

无论如何,以下是我的最佳拍摄:

from inv in invoices
group inv by new { inv.user_id } into userInv
join usr in users on userInv.Key.user_id equals usr.user_id
where usr.company_user.company_id = 1
select new
{
    usr.firstname,
    usr.lastname,
    amount = inv.Count(),
    sum = inv.Sum(amt => amt.amount)
}

至于LINQ的建议,我绝对建议您下载并使用LINQPad。我一直在使用它来测试没有Visual Studio的LINQ语句。它还会将您的LINQ声明转换为SQL,这可能是您特别感兴趣的。

编辑:Enigmatity的语句比我的语句更接近您的要求。我在使用的示例中没有偶然发现列分组。





相关问题
Manually implementing high performance algorithms in .NET

As a learning experience I recently tried implementing Quicksort with 3 way partitioning in C#. Apart from needing to add an extra range check on the left/right variables before the recursive call, ...

Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How do I compare two decimals to 10 decimal places?

I m using decimal type (.net), and I want to see if two numbers are equal. But I only want to be accurate to 10 decimal places. For example take these three numbers. I want them all to be equal. 0....

Exception practices when creating a SynchronizationContext?

I m creating an STA version of the SynchronizationContext for use in Windows Workflow 4.0. I m wondering what to do about exceptions when Post-ing callbacks. The SynchronizationContext can be used ...

Show running instance in single instance application

I am building an application with C#. I managed to turn this into a single instance application by checking if the same process is already running. Process[] pname = Process.GetProcessesByName("...

How to combine DataTrigger and EventTrigger?

NOTE I have asked the related question (with an accepted answer): How to combine DataTrigger and Trigger? I think I need to combine an EventTrigger and a DataTrigger to achieve what I m after: when ...

热门标签