2009-06-03 13 views
0

私はここでは少しブロックしていますが、私が最終的にやりたいことは、クエリに基づいてレコードセットを作成し、オブジェクト(Fooと呼ぶ)を作成し、同じIDを持つすべてのFooオブジェクトをArrayListのBarオブジェクトにグループ化するための新しいクエリを作成します。私はLinqからSQLにこれを行う方法はどうですか?既存のクエリからレコードセットを照会するLinq to Sql

public class Foo{ 
    public int id{get;set;} 
    public string name{get;set;} 
} 

public class Bar{ 
    public ArrayList foos{get;set;} 
} 

var query = from tFoo in fooTable join tFoo2 in fooTable2 on tFoo.id equals tFoo2.id 
      where tFoo2.colour = 'white' 
      select new Foo 
      { 
       id = tFoo.idFoo, 
       name = tFoo.name 
      }; 

var query2 = //iterate the first query and find all Foo objects with the the same 
      //tFoo.idFoo and store them into Bar objects 

最後に、Fooオブジェクトのリストを持つBarオブジェクトのレコードセットが必要です。

+0

ありますか? – cdonner

+0

とIQueryableに色を問い合わせたい場合は、色をFooクラスに追加する必要があります。 – cdonner

+0

arrayListの理由は、各バーに複数のfooがあるということです。 – Ayo

答えて

1

1つのバーまたは複数のバーが必要かどうかを判断するのは難しいですが、ここに提供された情報で私の最高のスタブです。

あなたを想定していた:

public class Foo 
{ 
    public int id {get;set;} 
    public string name {get;set;} 
    public string colour {get;set;} 
} 

public class Bar 
{ 
    public int id {get;set;} 
    public List<Foo> Foos {get;set;} 
} 

は、次に、あなたが行うことができます:あなたはArrayListにしたい特別な理由が

//executes the query and pulls the results into memory 
List<Foo> aBunchOfFoos = 
(
    from foo in db.Foos 
    where foo.colour == "white" 
    select foo 
).ToList(); 

// query those objects and produce another structure. 
// no database involvement 
List<Bar> aBunchOfBars = aBunchOfFoos 
    .GroupBy(foo => foo.id) 
    .Select(g => new Bar(){id = g.Key, Foos = g.ToList() }) 
    .ToList(); 
+0

これはfooオブジェクトを適切に作成してバーオブジェクトに格納しますが、リスト内の最初のクエリと同じIDをまとめてグループ化するアルゴリズムは動作しません – Ayo

+0

どのように動作しませんか?私はこのテクニックを何度も使ってきました。 –

関連する問題