English 中文(简体)
Linq to SQL With Left Outer Join and Group By With Sum - How To
原标题:

I m trying to transform the SQL Query below into Linq to SQL

select Categorias.IdCategoria, Categorias.Nome, SUM(lancamentos.valor)
from   lancamentos
left outer join Categorias on Lancamentos.IdCategoria = Categorias.IdCategoria
where  Month(DataLancamento) = 11
and    Credito = 1
and    Lancamentos.Ocultar = 0
group by Categorias.IdCategoria, Categorias.Nome

This is what I ve done

from    lancamento in Lancamentos
where   lancamento.Credito == true
&&      lancamento.Ocultar == false
&&      lancamento.DataLancamento.Month == 10
join    categoria in Categorias on lancamento.IdCategoria equals categoria.IdCategoria into temp
from    lancamentoJoinCategoria in temp.DefaultIfEmpty()
group   lancamentoJoinCategoria by new { lancamentoJoinCategoria.IdCategoria, lancamentoJoinCategoria.Nome } into x
select  new {
        IdCategoria = (int?)x.Key.IdCategoria
        , Nome = x.Key.Nome
}

How do I add the SUM(lancamentos.valor) to the linq to sql above ?

问题回答

It will be:

(from lancamento in Lancamentos
join categoria in Categorias on lancamento.IdCategoria equals categoria.IdCategoria into temp
from lancamentoJoinCategoria in temp.DefaultIfEmpty()

where lancamento.Credito == true
&& lancamento.Ocultar == false
&& lancamento.DataLancamento.Month == 10

group lancamento by new { lancamentoJoinCategoria.IdCategoria, lancamentoJoinCategoria.Nome } into x
select new
{
    IdCategoria = (int?)x.Key.IdCategoria,
    Nome = x.Key.Nome,
    sumValor = x.Sum(a=>a.valor)
});

You use the .Sum() method.

Eg;

Public Sub LinqToSqlCount03()
    Dim q = (From o In db.Orders _
        Select o.Freight).Sum()

Console.WriteLine(q)
End Sub

according to MSDN there is no query expression equivalent to the Sum() operation. I provided a little sample how you could use the Method Syntax of Sum() in a query.

Some query operations, such as Count or Max, have no equivalent query expression clause and must therefore be expressed as a method call. Method syntax can be combined with query syntax in various ways. For more information, see LINQ Query Syntax versus Method Syntax (C#).

var example = new[]
    {
        new { Count = 1, Name = "a" }, new { Count = 2, Name = "b" },
        new { Count = 2, Name = "c" }, new { Count = 2, Name = "c" }
    };

var result = from x in example
                select new 
                {
                    x.Name, 
                    Sum = (from y in example 
                           where y.Count.Equals(2) 
                               && y.Name==x.Name
                           select y.Count).Sum()
                };
var distinct = result.Distinct().ToList();




相关问题
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. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签