Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在.NET 5中的可枚举内填充可枚举_C#_Linq_.net Core_Ienumerable_Enumeration - Fatal编程技术网

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;
    }
}