2011-08-07 142 views
26

您好我能做到這一點的方法的語法,但我想提高我的技能拉姆達我該怎麼辦:LINQ 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(); 

回答

55

一般的例子:

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

這個例子顯示瞭如何遍歷分組值拿到鑰匙和彙總,以及如何獲得直接總數(如以前)。兩者都只使用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; 
    } 
} 
1

很少組通過實施例

公共無效GroupBy1() { 變種personList = dbEntities.People.GroupBy(M => m.PersonType)。選擇(M =>新的{PersonType =米.KEY,計數= 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(), 
       }); 
    }