LINQ Lambda Group By with Sum

guz6ccqo  于 2023-04-27  发布在  其他
关注(0)|答案(6)|浏览(146)

嗨,我可以在方法语法中做到这一点,但我正在努力提高我的lambda技能,我该怎么做:

SELECT SUM([job_group_quota]) as 'SUM'
FROM [dbo].[tbl_job_session]
WHERE [job_group_job_number] = @jobnum
and [job_group_ID] like @sess
GROUP BY [job_group_job_number]

我一直在摆弄它,但就是弄不好。

lnq.tbl_job_sessions.GroupBy(a => a.job_group_job_number == jnum)
                    .Select(b => new { b.job_group_quota}).Sum();
eit6fx6z

eit6fx6z1#

一般示例:

query
.GroupBy(item => item.GroupKey)
.Select(group => group.Sum(item => item.Aggregate));
ldxq2e6h

ldxq2e6h2#

少数按示例分组
public void GroupBy1(){ var personList = dbEntities.People.GroupBy(m =〉m.PersonType).Select(m =〉new { PersonType = m.Key,Count = m.Count()});}

public void GroupBy2()
    {
        var personList = dbEntities.People.GroupBy(m => new { m.PersonType, m.FirstName }).Select(m => new { PersonType = m.Key, Count = m.Count() });
    }

    public void GroupBy3()
    {
        var personList = dbEntities.People.Where(m => m.EmailPromotion != 0).GroupBy(m => new { m.PersonType, m.FirstName }).Select(m => new { PersonType = m.Key, Count = m.Count() });
    }

    public void GroupBy4()
    {
        var personList = dbEntities.People.GroupBy(m => new { m.PersonType, m.FirstName }).Where(m => m.Count() > 70).Select(m => new { PersonType = m.Key, Count = m.Count() });
    }

    public void GroupBy5()
    {
        var personList = dbEntities.People
            .GroupBy(m =>
                new
                {
                    m.PersonType
                }).Where(m => m.Count() > 70)
                    .Select(m =>
                        new
                        {
                            PersonType = m.Key,
                            Count = m.Count()
                        });

        var list1 = dbEntities.People.
            GroupBy(m => new { m.PersonType }).
            Select(m =>
                new
                {
                    Type = m.Key,
                    Count = m.Count()
                })
            .Where(
                   m => m.Count > 70
                && m.Type.PersonType.Equals("EM")
                || m.Type.PersonType.Equals("GC"));
    }

    public void GroupBy6()
    {
        var list1 = dbEntities.People.
            GroupBy(m => new { m.PersonType, m.EmailPromotion }).Select(m =>
                new
                {
                    Type = m.Key,
                    Count = m.Count()
                })
            .Where
            (
                m => m.Count > 70 && m.Type.EmailPromotion.Equals(0) &&
                (
                    m.Type.PersonType.Equals("EM") ||
                    m.Type.PersonType.Equals("GC")
                ));
    }

    public void GroupBy7()
    {
        var list1 = dbEntities.People.
            GroupBy(m => m.PersonType).
            Select(c =>
                new
                {
                    Type = c.Key,
                    Total = c.Sum(p => p.BusinessEntityID)
                });
    }

    public void GroupBy8()
    {
        var list1 = dbEntities.People.
            GroupBy(m => m.PersonType).
            Select(c =>
                new
                {
                    Type = c.Key,
                    Count = c.Count(),
                    Total = c.Sum(p => p.BusinessEntityID)
                });
    }

    public void GroupBy9()
    {
        var list1 = dbEntities.People.
            GroupBy(m => m.PersonType).
            Select(c =>
                new
                {
                    Type = c.Key,
                    Max = c.Max(),
                });
    }
but5z9lq

but5z9lq3#

如果你想得到一个key-to-sum Dictionary结果。

var allJobQuota = jobSessions.GroupBy(s => s.jobNumber)
                             .ToDictionary(g => g.Key, g => g.Sum(s => s.quota));
fgw7neuy

fgw7neuy4#

这个例子展示了如何迭代分组的值,获得键和总数,以及如何直接获得总数(像前面一样)。两者都只使用lambda运算符。

using System;
using System.Collections.Generic;
using System.Linq;

public class Person
{
    public string Name { get; set; }
    public string City { get; set; }
    public string ZipCode { get; set; }
    public int SomeValue { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<Person> data = GetPopulatedData();

        var totals = data.GroupBy(x =>
            new { x.Name, x.City, x.ZipCode }).Select(y =>
                y.Sum(i => i.SomeValue));

        var groupsForIterate = data.GroupBy(x =>
            new { x.Name, x.City, x.ZipCode });

        Console.WriteLine("Totals: ");
        foreach (var total in totals)
        {
            Console.WriteLine(total);
        }

        Console.WriteLine("Categories: ");
        foreach (var categ in groupsForIterate)
        {
            // You can refer to one field like this: categ.Key.Ciduad
            Console.WriteLine("Group" + categ.Key);
            Console.WriteLine(categ.Sum(x => x.SomeValue));
        }

        //Output:
        //Totals:
        //1
        //2
        //1
        //Categories:
        //Group{ Name = Mark, City = BCN, ZipCode = 00000 }
        //1
        //Group{ Name = Mark, City = BCN, ZipCode = 000000 }
        //2
        //Group{ Name = John, City = NYC, ZipCode = 000000 }
        //1
    }

    private static List<Person> GetPopulatedData()
    {
        List<Person> datos = new List<Person>()
        {
            new Person(){Name="Mark", City = "BCN",
                ZipCode = "00000", SomeValue = 1}, // group A

            new Person(){Name="Mark", City = "BCN",
                ZipCode = "000000", SomeValue = 1}, // group B

            new Person(){Name="Mark", City = "BCN",
                ZipCode = "000000", SomeValue = 1}, // group B

            new Person(){Name="John", City = "NYC",
                ZipCode = "000000", SomeValue = 1}, // group C
        };
        return datos;
    }
}
bvn4nwqk

bvn4nwqk5#

Sum Ficha_Venda and Entrada from Movimento:

var query = from bd in db.Movimento
                            where (movimento.Data != null ? bd.Data == movimento.Data : bd.Data == movimento.Data)
                            && (bd.Loja == Loja)
                            group bd by bd.Data into t
                            select new {entrada = t.Sum(bd=> bd.Entrada), ficha = t.Sum(bd=> bd.Ficha_Venda)};
xkrw2x1b

xkrw2x1b6#

lnq.tbl_job_sessions.Where(x => x.job_group_job_number == @jobnum 
                          && x.job_group_ID.Contains(""+ @sess +"")
                        )
                 .GroupBy(a => a.job_group_job_number)
                 .Select(b => new 
                              { 
                                SUM = b.Sum( p => p.job_group_quota)
                              }
                        );

相关问题