C# 在.NET 5中的可枚举内填充可枚举
我有一个“面板”模型,它有多个“面板页面”。我想得到所有面板的列表,并用各自的“面板页面”填充每个面板 这是我目前的代码(有效):C# 在.NET 5中的可枚举内填充可枚举,c#,linq,.net-core,ienumerable,enumeration,C#,Linq,.net Core,Ienumerable,Enumeration,我有一个“面板”模型,它有多个“面板页面”。我想得到所有面板的列表,并用各自的“面板页面”填充每个面板 这是我目前的代码(有效): public IEnumerable GetCustomPanels() { var customPanels=_customPanelService.getdynamicccustompanels(); var dynamicccustompanels=customPanels.ToList(); foreach(dynamiccustompanel.ToList
public IEnumerable GetCustomPanels()
{
var customPanels=_customPanelService.getdynamicccustompanels();
var dynamicccustompanels=customPanels.ToList();
foreach(dynamiccustompanel.ToList()中的var customPanel)
{
var customPanelPages=_customPanelPageService.GetCustomPanelPages(customPanel.PanelGUID.ToString());
customPanel.CustomPanelPages=CustomPanelPages;
}
返回动态公司;
}
如何以最少的行数完成此操作?这应该可以:
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
return _customPanelService.GetDynamicCustomPanels().Select(p => {
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
return p;
});
}
这是。。。还有3条语句(计数
foreach
)和一个块,只是间隔不同,以使用一行文字。如何在最少的行数内完成此操作?
是否存在问题,可能是维护和/或性能问题?摆脱那些额外的ToList()
调用。他们耗尽了记忆。
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
return _customPanelService.GetDynamicCustomPanels().Select(p => {
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
return p;
});
}
public IEnumerable<DynamicCustomPanel> GetCustomPanels()
{
foreach(var p in _customPanelService.GetDynamicCustomPanels())
{
p.CustomPanelPages = _customPanelPageService.GetCustomPanelPages(p.PanelGUID.ToString());
yield return p;
}
}