2009-06-03 91 views
0

我在這裏有一點路障,但我最終想做的是根據查詢創建一個記錄集並將信息存儲到單獨的對象(讓我們稱它們爲Foo),然後創建一個新的查詢,將具有相同ID的所有Foo對象分組成一個ArrayList到Bar對象中。我如何去Linq to 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 

那麼,到底我應該有吧對象的記錄與富貴對象的列表。

+0

爲什麼你想一個ArrayList有什麼特別的原因? – cdonner 2009-06-03 17:54:15

+0

,如果你想查詢你的IQueryable的顏色,你將不得不添加顏色到你的Foo類。 – cdonner 2009-06-03 17:55:09

回答

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;} 
} 

然後,你可以這樣做:

//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();