-1

Class A has following properties:

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

The collection of A that contains:

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

A_Id = 2, B_Ids = {104}

A_Id = 3, B_Ids = {105, 106}

From the collection above, I would like to create a new collection with this content:

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

How could I do that by using linq?

Regards, Mike

manojlds
  • 259,347
  • 56
  • 440
  • 401
Mike
  • 29
  • 3

2 Answers2

4

Use SelectMany:

var result = list.SelectMany(a => a.B_Ids.Select(b => new { a.A_Id, B_Id = b }));
manojlds
  • 259,347
  • 56
  • 440
  • 401
-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}});
mrtgold
  • 61
  • 6