开发者

loop one sequence and check againt other list

开发者 https://www.devze.com 2023-01-04 20:48 出处:网络
i have a namespace string like \"Company.Product.Sub1.Sub2.IService\". The Sub1/Sub2 can differ in their count, but normally their is one part which matches to

i have a namespace string like "Company.Product.Sub1.Sub2.IService". The Sub1/Sub2 can differ in their count, but normally their is one part which matches to a Dictionary with AssemblyFullname as key and path to it as value.

Now ive written this code

        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;
            }     开发者_JAVA百科   
        }

Works but doesnt look nice in my opinion.

I tried it with linq but i dont know how ive to write it.

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

Thanks for help.


To first put it into English, you're trying to go through the parts (backwards) of the Fullname in interfaceCodeElement and find the first that matches (as a substring, case-sensitive) any of the keys in references (which is a Dictionary<string, string> from fullname to path). Your result, type, is a KeyValuePair<string, string> although it's not clear if you actually need that (both the key and value) or just one or the other.

One, it seems a little odd to have a Dictionary in such a case, since you're not able to lookup as a key, but I guess it still works for the purpose :) Switching to something like List<Tuple<string, string>> or List<KeyValuePair<string, string>> might make sense, as the order of the pairs that comes from iteration over references will potentially affect which pair is selected into type.

In order to try to make it easier to understand, I'll add a let here:

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>();


This should give you a list of the matches:

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

Edit: So based on your comments I think I kind of understand. Assuming you have some list of these fullNames somewhere:

// 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消