0

A 类具有以下属性:

public int A_Id { get; set; }
public IEnumerable<int> B_Ids { get; set; }

A 的集合包含:

A_Id = 1,B_Ids = {101、102、103}

A_Id = 2,B_Ids = {104}

A_Id = 3,B_Ids = {105, 106}

从上面的集合中,我想创建一个包含以下内容的新集合:

A_Id = 1,B_Id = 101

A_Id = 1,B_Id = 102

A_Id = 1,B_Id = 103

A_Id = 2,B_Id = 104

A_Id = 3,B_Id = 105

A_Id = 3,B_Id = 106

我怎么能通过使用 linq 做到这一点?

问候,迈克

4

2 回答 2

4

使用SelectMany

var result = list.SelectMany(a => a.B_Ids.Select(b => new { a.A_Id, B_Id = b }));
于 2013-01-08T17:55:09.367 回答
-1
class ClassA
{
    public int A_Id { get; set; }
    public IEnumerable<int> B_Ids { get; set; }
}

var newAs = (from a in As from bId in a.B_Ids select new ClassA() {A_Id = a.A_Id, B_Ids = new int[] {bId}});
于 2013-01-08T17:51:06.093 回答