代码之家  ›  专栏  ›  技术社区  ›  Vikas Gupta

代码[关闭]的lambda表达式等价物是什么

  •  -2
  • Vikas Gupta  · 技术社区  · 6 年前

    我有这段代码,不知道它的lambda表达式是什么。我试过了,事实上,我读过了同样的高级教程,仍然无法从注释行开始理解以下代码的lambda表达式。

    IDictionary<string, GitItem> mappedPathToGitItems = new Dictionary<string, GitItem>();
    
    mappedPathToGitItems = clientWrapper.GetFilePathToGitItems(
            gitLatestCommit, versionDescriptor, mappedPath, maxBatchSize);
    
    List<string> filepaths = new List<string>();
    
    // Lambda expression starts from here
    // filepaths = {Lambda expression of the below code.}
    foreach(KeyValuePair<string, GitItem> entry in mappedPathToGitItems)
    {
        string item = entry.Key;
        GitItem gitItem = entry.Value;
    
        if(gitItem != null)
        {
            filepaths.Add(item);
        }
    }
    // Ends here
    
    2 回复  |  直到 6 年前
        1
  •  5
  •   Lee    6 年前
    List<string> filepaths = mappedPathToGitItems
        .Where(kvp => kvp.Value != null)
        .Select(kvp => kvp.Key)
        .ToList();
    
        2
  •  0
  •   Ousmane D.    6 年前

    当你说“lambda表达式等价物”时,我假设你的意思是通过LINQ:

    List<string> filepaths = mappedPathToGitItems.Where(item => item.Value != null)
                                                 .Select(item => item.Key)
                                                 .ToList();