views:

66

answers:

2

Hi,

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

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.

A: 

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();
Kelsey
ok, with a litte bit of modification in the where on referencesrefs.Where(r => r.Key.Contains(fp)).Count() != 0) But i need the result in other way i need the references as result.
K.Hoffmann
@k-hoffmann I don't understand why you need that `Count()` check. The `fullNameParts` item will only be included in the result if at least one item in `references` matches.
Kelsey
The modification are needed cause one bracket was missing in your sample. And the Count modification cause the inner where doesnt return a bool for the outer where.I need the KeyValuePair of the references, fullNameParts is the namespace string splitted.If i try that way you show on the dictionary i dont know how i can build the inner where cause i have to loop the fullNameParts array and do "r => r.Contains(sequenceItemOfFullNameParts))".Sorry if i was not clear enough.
K.Hoffmann
A: 

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>();
James Manning
Hi,first sorry for my late answer and thanks for puting my words into understanable sentences.You're right with your explanation, thats what i want to do.Have a nice day, thanks all
K.Hoffmann