2011-12-19 23 views
5

彼女は私が持っているテーブルを示すイメージです、私は表示する必要があるグリッドです。 さんは画像を投稿できません。説明してみてください。私のテーブルには4つの列があります。Linq To SQL Group By and Sum

  1. プロジェクト番号(文字列)
  2. ItemNumber(String)を
  3. 場所(String)を
  4. 数量(レアル)。

グリッドがこのように表示される必要があります。

  1. ProjectNumber
  2. QtyMain
  3. ItemNumber。
  4. 数量その他。私はSE 1つのラインのPRプロジェクト/ ItemNumber組み合わせ合算数量は場所がメインで、1場所ではない数量を示す数量に1件2つのdifferetnのcolumsになりますので、エヴリー行をグループ化するLINQクエリを記述する必要が

(!=)main。私にとってlinqがこれを行うことはできますか、それともどのようにして行うことができますか?

に結果の
+0

(http://stackoverflow.com/questions/216513/sum-and-group -by-in-linq-to-sql) –

+1

私はそれはあるが、多分それは私がVBをよく知っていないので、それはできません。 –

答えて

7
public class Foo 
{ 
    public Int32 ProjectNumber; 
    public String ItemNumber; 
    public String InventLocation; 
    public Int32 Qty; 
} 

void Main() 
{ 
    List<Foo> foos = new List<Foo>(new[]{ 
     new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Main", Qty = 3 }, 
     new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Main", Qty = 3 }, 
     new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Sub", Qty = 2 }, 
     new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Sub", Qty = 1 }, 
     new Foo { ProjectNumber = 1, ItemNumber = "a", InventLocation = "Sub2", Qty = 5 } 
    }); 

    var foo = from f in foos 
       group f by new { f.ProjectNumber, f.ItemNumber } into fGroup 
       select new { 
       ProjectNumber = fGroup.Key.ProjectNumber, 
       ItemNumber = fGroup.Key.ItemNumber, 
       QtyMain = fGroup.Where (g => g.InventLocation == "Main").Sum (g => g.Qty), 
       Rest = fGroup.Where (g => g.InventLocation != "Main").Sum (g => g.Qty) 
       }; 
    foo.Dump(); 
} 

:[?LINQ to SQLはで合計とグループ化]これはの複製である

IEnumerable<> (1 item) 
ProjectNumber ItemNumber QtyMain Rest 
1    a   6  8 
+1

助けのためのTy、excactly私が必要としたもの。 THX :) –

+0

@Scarface:どうぞよろしくお願いします。私は助けてうれしいです。 –

+0

@Scarface:BTW、LINQpadを見て、私は(あなたが始めているのであればそれを知りたいのですが)ぜひお勧めします。上記のツールはコピー&ペーストして、便利な方法を表示します(エディタウィンドウの上の "Language:C#program"を必ず選択してください)。 –