2012-02-27 81 views
1

我如何在C#中聚集有條件如何做到這一點在C#

如以下VB代碼:

Dim movement = From item In dbo.VIEWITEMMOVEMENTs 
       Where item.DATETRANSFERRED.Value.Date = Me.DateTimePicker1.Value.Date 
       Group By item.DATETRANSFERRED.Value.Date, item.ITEMDESCRIPTION 
       Into moved = Group, sold = Sum(item.QUANTITYSOLD), [IN] = Sum(If(item.QUANTITY < 0.0, 0.0, item.QUANTITY)), Out = Sum(If(item.QUANTITY > 0, 0, item.QUANTITY)) 
       Select ITEMDESCRIPTION, sold, [IN], Out Order By ITEMDESCRIPTION Ascending 
Me.DataGridView1.DataSource = movement 
+5

你試過了什麼? C#幾乎完全相同。 – Oded 2012-02-27 16:00:01

+0

這類問題並不適合本網站。主題行也沒什麼用處 - 這是搜索證明。 – 2012-02-27 16:07:59

回答

0
var result = (
       from item in dbo.VIEWITEMMOVEMENTs 
       where item.DATETRANSFERRED.Value.Date == this.DateTimePicker1.Value.Date 
       group item by new { item.DATETRANSFERRED.Value.Date, item.ITEMDESCRIPTION } 
       into g 
       select new 
       { 
        g.Key.ITEMDESCRIPTION, 
        sold = g.Sum(x => x.QUANTITYSOLD), 
        IN = g.Sum(x => (x.QUANTITY <0 ? 0 : x.QUANTITY)), 
        Out = g.Sum(x => (x.QUANTITY > 0 ? 0 : x.QUANTITY)) 
       } 
       ).OrderBy(d => d.ITEMDESCRIPTION); 
0

,如果你正在嘗試的代碼片段轉換。您可以使用轉換器像

VB to C#

希望這有助於。