【问题标题】:loop one sequence and check againt other list循环一个序列并检查另一个列表
【发布时间】:2011-03-09 08:17:00
【问题描述】:

我有一个命名空间字符串,例如“Company.Product.Sub1.Sub2.IService”。 Sub1/Sub2 的计数可能不同,但通常它们是匹配的一部分 以 AssemblyFullname 为键,路径为值的字典。

现在我写了这段代码

        string fullName = interfaceCodeElement.FullName;
        var fullNameParts = interfaceCodeElement.FullName.Split('.').Reverse();            
        KeyValuePair<string, string> type = new KeyValuePair<string,string>();

        foreach (var item in fullNameParts)
     {
         var match = references.Where(x => x.Key.Contains(item)).ToList();

            if (match.Count > 0)
            {
                type = match[0];
                break;
            }        
        }

有效,但在我看来并不好看。

我用 linq 试过了,但我不知道怎么写。

var matches = from reference in refs
              where reference.Key.Contains(fullNameParts.Reverse().

感谢您的帮助。

【问题讨论】:

    标签: c# .net linq .net-3.5 lambda


    【解决方案1】:

    首先要把它写成英文,你试图通过 interfaceCodeElement 中 Fullname 的部分(向后)并找到第一个匹配(作为子字符串,区分大小写)引用中的任何键(其中是从全名到路径的Dictionary&lt;string, string&gt;)。您的结果typeKeyValuePair&lt;string, string&gt;,但尚不清楚您是否真的需要它(键和值)或只是其中一个。

    一个,在这种情况下拥有字典似乎有点奇怪,因为您无法将其作为键进行查找,但我想它仍然可以用于此目的:) 切换到类似 List&lt;Tuple&lt;string, string&gt;&gt;List&lt;KeyValuePair&lt;string, string&gt;&gt; 可能是有道理的,因为来自迭代引用的对的顺序可能会影响选择哪个对。

    为了尽量让大家更容易理解,我这里加个let

    var bestMatchPerPart = from part in fullNameParts
                           let firstMatchPair = references.FirstOrDefault(pair => pair.Key.Contains(part))
                           where firstMatchPair != null // ignore parts that have no match
                           // since we want the pair, not the part, select that
                           select firstMatchPair;
    
    var type = bestMatchPerPart.FirstOrDefault()
               // to match original behavior, empty pair in result instead of null if no match
               ?? new KeyValuePair<string, string>();
    

    【讨论】:

    • 嗨,首先对我迟到的回答感到抱歉,感谢您将我的话变成可以理解的句子。你的解释是对的,这就是我想要做的。祝你有美好的一天,谢谢大家
    【解决方案2】:

    这应该会给你一个匹配的列表:

    var listOfMatches = fullNameParts.Where(fp => references.Where(r => r.Key.Contains(fp))).ToList();
    

    编辑:因此,根据您的 cmets,我想我有点理解。假设你在某处有这些fullNames 的列表:

    // Making this up because I am nor sure what you have to start with
    List<string> yourListOfAllYourFullNames = GetThisList();
    
    var listOfMatches = yourListOfAllYourFullNames.Where(
        fnl => fnl.Split('.').Reverse().Where(
           fnp => references.Where(r => r.Key.Contains(fnp))).Count() > 0).ToList();
    

    【讨论】:

    • 好的,在引用 refs.Where(r => r.Key.Contains(fp)).Count() != 0) 的 where 中稍作修改,但我需要结果以其他方式,我需要引用作为结果。
    • @k-hoffmann 我不明白你为什么需要Count() 检查。仅当references 中的至少一项匹配时,fullNameParts 项才会包含在结果中。
    • 需要修改,因为您的示例中缺少一个括号。并且 Count 修改导致内部 where 不会为外部 where 返回布尔值。我需要引用的 KeyValuePair,fullNameParts 是拆分的命名空间字符串。如果我尝试你在字典上显示的那种方式,我不知道如何构建内部,因为我必须循环 fullNameParts 数组并执行“r => r.Contains(sequenceItemOfFullNameParts))”。对不起,如果我不够清楚。
    猜你喜欢
    • 2016-12-24
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-30
    • 2019-09-01
    • 1970-01-01
    相关资源
    最近更新 更多