gpt4 book ai didi

c# - Linq 查询 - 列表中的列表

转载 作者:太空宇宙 更新时间:2023-11-03 12:25:06 24 4
gpt4 key购买 nike

<分区>

我正在尝试选择一个包含 Fund.Name 的列表和 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 }
};

然后我尝试用这个创建查询:

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

我想要这样的东西:

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

但是我得到这样的结果:

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

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com