基于相同的 id 将列表模型转换为另一个列表模型
convert list model to another list model based on same id
我有以下型号的列表
public class Participant{
public int Id{ get; set; }
public string Name{ get; set; }
public string Address{ get; set; }
}
输入
Id Name Adress
1 Jhon America
2 Jhon Indonesia
3 Jhon Jerman
4 Okto Africa
4 Okto Arabic
5 Syahrul India
如何根据 id
将该列表转换为以下模型
public class Participant{
public int Id{ get; set; }
public string Name{ get; set; }
public List<Address> Address{ get; set; }
}
public class Address{
public string Name{ get; set; }
}
所以结果会是这样
Id Name Adress
1 Jhon America,Indonesia,Jerman
4 OKTO ARABIC,Africa
5 Syahrul India
我的方法是使用 for 循环,当 id 相同时,我将添加地址
如果然后跳到下一个 id
但我没有做到
有什么好的有效方法可以做到这一点
您可以按 Id
和 Name
分组。然后你需要从每个组中 select 一个你的新模型的实体。您可以从分组键中获得的 ID 和名称以及地址必须 select 自己编辑:
List<Participant_New> newModelCollection = participantCollection
.GroupBy(x => new {Id = x.Id, Name = x.Name})
.Select(g => new Participant_New
{
Id = g.Key.Id,
Name = g.Key.Name,
Address = g.Select(a => new Address{Name = a.Address}).ToList()
}).ToList();
如果您更喜欢不太繁重的 Linq 解决方案,您可以执行以下操作。
List<Participant> participants = new List<Participant>();
List<Participant2> participant2s = new List<Participant2>();
foreach (var participant in participants)
{
Participant2 par = participant2s.FirstOrDefault(x => x.Id == participant.Id);
if (par is null)
{
participant2s.Add(new Participant2
{
Id = participant.Id,
Name = participant.Name,
Adresses = new List<Address> { new Address { Name = participant.Address } }
});
}
else
{
par.Adresses.Add(new Address { Name = participant.Address });
}
}
备注
与其他解决方案相比,您可能更喜欢此解决方案,因为至少在我看来,它更易于阅读。此外,如果你变得更复杂,这将更具可读性。
说明
- 这很简单,在这种情况下,您可以创建新模型的新列表
Participant2
。
- 之后,您只需循环遍历包含 'old' 个模型的第一个列表中的所有条目。
- 您检查您的列表中是否已有包含具有相同 ID 的项目的条目。
- 如果没有匹配项,它只会创建一个包含相同信息的新项目。
- 如果有一个匹配,它会将地址添加到条目中。
重要信息
如果其中一个列表是 DbSet<T>
或类似列表,您应该非常小心,因为这段代码不会在数据库本身上执行,这确实会损害性能。
我有以下型号的列表
public class Participant{
public int Id{ get; set; }
public string Name{ get; set; }
public string Address{ get; set; }
}
输入
Id Name Adress
1 Jhon America
2 Jhon Indonesia
3 Jhon Jerman
4 Okto Africa
4 Okto Arabic
5 Syahrul India
如何根据 id
将该列表转换为以下模型public class Participant{
public int Id{ get; set; }
public string Name{ get; set; }
public List<Address> Address{ get; set; }
}
public class Address{
public string Name{ get; set; }
}
所以结果会是这样
Id Name Adress
1 Jhon America,Indonesia,Jerman
4 OKTO ARABIC,Africa
5 Syahrul India
我的方法是使用 for 循环,当 id 相同时,我将添加地址 如果然后跳到下一个 id
但我没有做到
有什么好的有效方法可以做到这一点
您可以按 Id
和 Name
分组。然后你需要从每个组中 select 一个你的新模型的实体。您可以从分组键中获得的 ID 和名称以及地址必须 select 自己编辑:
List<Participant_New> newModelCollection = participantCollection
.GroupBy(x => new {Id = x.Id, Name = x.Name})
.Select(g => new Participant_New
{
Id = g.Key.Id,
Name = g.Key.Name,
Address = g.Select(a => new Address{Name = a.Address}).ToList()
}).ToList();
如果您更喜欢不太繁重的 Linq 解决方案,您可以执行以下操作。
List<Participant> participants = new List<Participant>();
List<Participant2> participant2s = new List<Participant2>();
foreach (var participant in participants)
{
Participant2 par = participant2s.FirstOrDefault(x => x.Id == participant.Id);
if (par is null)
{
participant2s.Add(new Participant2
{
Id = participant.Id,
Name = participant.Name,
Adresses = new List<Address> { new Address { Name = participant.Address } }
});
}
else
{
par.Adresses.Add(new Address { Name = participant.Address });
}
}
备注
与其他解决方案相比,您可能更喜欢此解决方案,因为至少在我看来,它更易于阅读。此外,如果你变得更复杂,这将更具可读性。
说明
- 这很简单,在这种情况下,您可以创建新模型的新列表
Participant2
。 - 之后,您只需循环遍历包含 'old' 个模型的第一个列表中的所有条目。
- 您检查您的列表中是否已有包含具有相同 ID 的项目的条目。
- 如果没有匹配项,它只会创建一个包含相同信息的新项目。
- 如果有一个匹配,它会将地址添加到条目中。
重要信息
如果其中一个列表是 DbSet<T>
或类似列表,您应该非常小心,因为这段代码不会在数据库本身上执行,这确实会损害性能。