SQLへの既存のクエリのLINQからレコードセットを照会します
-
09-09-2019 - |
質問
私は、ここでは、道路のブロックの少しでんだけど、私は最終的にやりたいことは、クエリのオフに基づいてレコードセットを作成し、個別のオブジェクトに情報を格納することである(のはFooのそれらを呼びましょう)を作成しますグループに新しいクエリーバーオブジェクトへのArrayListに同じIDの持つすべてのFooオブジェクト。どのように私は、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のオブジェクトのリストにバーオブジェクトのレコードを持っている必要があります。
解決
それはあなたが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();
所属していません StackOverflow