我在这里遇到了一些障碍,但我最终要做的是创建一个基于查询的记录集并将信息存储到单独的对象中(让我们称之为Foo)然后创建一个新查询将具有相同id的所有Foo对象分组到一个ArrayList到Bar对象中。我将如何在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 :(得分: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();