C# SQL到Linq等价物

C# SQL到Linq等价物,c#,linq,C#,Linq,如何将以下SQL转换为LINQ select sum(salary),Deptid from person where deptid=1 or deptid=2 group by deptid 下面是一个完整的示例: using System; using System.Collections.Generic; using System.Linq; class Program { class Person { public int DeptId { get;

如何将以下SQL转换为LINQ

select sum(salary),Deptid from person where deptid=1 or deptid=2 group by
deptid 
下面是一个完整的示例:

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

class Program
{
    class Person
    {
        public int DeptId { get; set; }
        public int Salary { get; set; }
    }

    static void Main(string[] args)
    {
        List<Person> persons = new List<Person>();
        persons.Add(new Person { DeptId = 1, Salary = 10000 });
        persons.Add(new Person { DeptId = 1, Salary = 20000 });
        persons.Add(new Person { DeptId = 1, Salary = 30000 });
        persons.Add(new Person { DeptId = 2, Salary = 40000 });
        persons.Add(new Person { DeptId = 3, Salary = 50000 });

        var salaries = persons
            .Where(p => p.DeptId == 1 || p.DeptId == 2)
            .GroupBy(p => p.DeptId)
            .Select(x => new { DeptId = x.Key, TotalSalary = x.Sum(p => p.Salary)});

        foreach (var dept in salaries)
            Console.WriteLine("{0}: {1}", dept.DeptId, dept.TotalSalary);
    }
}
下面是一个完整的示例:

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

class Program
{
    class Person
    {
        public int DeptId { get; set; }
        public int Salary { get; set; }
    }

    static void Main(string[] args)
    {
        List<Person> persons = new List<Person>();
        persons.Add(new Person { DeptId = 1, Salary = 10000 });
        persons.Add(new Person { DeptId = 1, Salary = 20000 });
        persons.Add(new Person { DeptId = 1, Salary = 30000 });
        persons.Add(new Person { DeptId = 2, Salary = 40000 });
        persons.Add(new Person { DeptId = 3, Salary = 50000 });

        var salaries = persons
            .Where(p => p.DeptId == 1 || p.DeptId == 2)
            .GroupBy(p => p.DeptId)
            .Select(x => new { DeptId = x.Key, TotalSalary = x.Sum(p => p.Salary)});

        foreach (var dept in salaries)
            Console.WriteLine("{0}: {1}", dept.DeptId, dept.TotalSalary);
    }
}

注意:裸头回答:-)注意:裸头回答:-)你的第二个问题怎么了?它被删除了。。。是你删除了它吗?我问是因为我要发布答案,然后StackOverflow告诉我不能,因为它被删除了。你的第二个问题怎么了?它被删除了。。。是你删除了它吗?我问是因为我要发布答案,然后StackOverflow告诉我我不能,因为它已经被删除了。
using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    class Person
    {
        public int DeptId { get; set; }
        public int Salary { get; set; }
    }

    static void Main(string[] args)
    {
        List<Person> persons = new List<Person>();
        persons.Add(new Person { DeptId = 1, Salary = 10000 });
        persons.Add(new Person { DeptId = 1, Salary = 20000 });
        persons.Add(new Person { DeptId = 1, Salary = 30000 });
        persons.Add(new Person { DeptId = 2, Salary = 40000 });
        persons.Add(new Person { DeptId = 3, Salary = 50000 });

        var salaries = persons
            .Where(p => p.DeptId == 1 || p.DeptId == 2)
            .GroupBy(p => p.DeptId)
            .Select(x => new { DeptId = x.Key, TotalSalary = x.Sum(p => p.Salary)});

        foreach (var dept in salaries)
            Console.WriteLine("{0}: {1}", dept.DeptId, dept.TotalSalary);
    }
}
1: 60000
2: 40000