将嵌套for循环转换为Linq

本文关键字:Linq 转换 循环 嵌套 for | 更新日期: 2023-09-27 18:06:28

可以将下面嵌套的for循环转换成linq查询吗?

我有如下的类定义:

 public class MessageCodes
{
    public string IdentityKey{ get;  set; }
    public Dictionary<string, string> LangCollection { get;  set; }
    public MessageCodes()
    {
        LangCollection = new Dictionary<string, string>();
    }
}

IdentityKey在上面的类中是唯一的,LangCollection可以有多个key, value。

 MessageCodes newCode = new MessageCodes()
                       {
                           MessageCode = "10",
                           LangCollection = new Dictionary<string, string>()
                           {
                               {"en","Hello"},
                               {"de","dello"},
                                {"fr","fello"},
                           }
                       };
        MessageCodes oldCode = new MessageCodes()
        {
            MessageCode = "10",
            LangCollection = new Dictionary<string, string>()
                           {
                               {"en","fello"},
                               {"de","pello"},
                               {"fr","fello"},
                           }
        };

上面是一个MessageCode Instance的例子。

现在我有NewMessageCodes和OldMessageCodes的集合。我需要比较它们下面是嵌套forloop的问题,

foreach (MessageCodes newMessageCode in newDiffMsgCodeCollection)
{
    foreach (MessageCodes oldMessageCode in oldDiffMsgCodeCollection)
    {
        if (newMessageCode.MessageCode == oldMessageCode.MessageCode)
        {
            MessageCodes msgCodes = new MessageCodes();
            msgCodes.MessageCode = newMessageCode.MessageCode;
            Dictionary<string, string> langCollection = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> newLangNode in newMessageCode.LangCollection)
            {
                foreach (KeyValuePair<string, string> oldLangNode in oldMessageCode.LangCollection)
                {
                    string newCode = newLangNode.Key.Trim().ToLower();
                    string oldCode = oldLangNode.Key.Trim().ToLower();
                    if (newCode  == oldCode )
                    {
                        if (newLangNode.Value != oldLangNode.Value)
                        {
                            langCollection.Add(newCode, oldCode);
                        }
                    }
                }
            }
            msgCodes.LangCollection = langCollection;
        }
    }
}

将上述代码转换为Linq查询或Lambda表达式。

将嵌套for循环转换为Linq

newDiffMsgCodeCollection.ForEach(pd => {
                oldDiffMsgCodeCollection.ForEach(pds =>
                {
                    if (pd.MessageCode == pds.MessageCode)
                    {
                      //business logic
                    }
                });
            });

我认为这就是你想要的:

var messageCodesQuery =
    from newMessageCode in newDiffMsgCodeCollection
    from oldMessageCode in oldDiffMsgCodeCollection
    where newMessageCode.MessageCode == oldMessageCode.MessageCode
    select new MessageCodes()
    {
        MessageCode = newMessageCode.MessageCode,
        LangCollection = (
            from newLangNode in newMessageCode.LangCollection
            from oldLangNode in oldMessageCode.LangCollection
            let newCode = newLangNode.Key.Trim().ToLower()
            let oldCode = oldLangNode.Key.Trim().ToLower()
            where newCode == oldCode
            where newLangNode.Value != oldLangNode.Value
            select new { newCode, oldCode }
        ).ToDictionary(x => x.newCode, x => x.oldCode)
    };

你现在只需要确保你对结果做了一些事情。