0

我在这里遇到了一点路障,但我最终想做的是根据查询创建一个记录集并将信息存储到单独的对象中(我们称它们为 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

所以,最后我应该有一个 Bar 对象的记录集和一个 Foo 对象的列表。

4

1 回答 1

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();
于 2009-06-03T17:52:04.487 回答