2

I'm trying to select a list that contains Fund.Name and List<Investment>.

var funds = new List<Fund>
{
    new Fund { Id = 1 , Name = "good" },
    new Fund { Id = 2, Name = "bad" }
};

var investments = new List<Investment>
{
    new Investment { Fund = funds[0], Value = 100 },
    new Investment { Fund = funds[0], Value = 200 },
    new Investment { Fund = funds[1], Value = 300 }
};

Then I'm trying to create the query with this:

var query = from f in funds
            join i in investments
            on f.Id equals i.Fund.Id
            select new { f.Name, i };

I wanted something like this:

{ Name = good, {{ Id = 1, Value = 100 }, { Id = 1, Value = 200 }}},
{ Name = bad, { Id = 2, Value = 300 }}

But I'm getting something like this:

{ Name = good, { Id = 1, Value = 100 }},
{ Name = good, { Id = 1, Value = 200 }},
{ Name = bad, { Id = 2, Value = 300 }}

1 Answers1

1

Try using GroupJoin.

var query = funds.GroupJoin(investments, f => f.Id, i => i.Fund.Id, (f, result) => new { f.Name, result });
Slai
  • 22,144
  • 5
  • 45
  • 53
Ayaz
  • 2,111
  • 4
  • 13
  • 16