tags:

views:

60

answers:

1

I have a list of multiple string and I need to do operation on them by the suffixe they have. The only thing that is not changing is the beginning of the string (They will be always ManifestXXX.txt, FileNameItems1XXX...). The string end's with a suffix is different everytime. Here is what I have so far (Linq Pad):

var filesName = new[] { "ManifestSUFFIX.txt",
"FileNameItems1SUFFIX.txt",
"FileNameItems2SUFFIX.txt",
"FileNameItems3SUFFIX.txt",
"FileNameItems4SUFFIX.txt",

"ManifestWOOT.txt",
"FileNameItems1WOOT.txt",
"FileNameItems2WOOT.txt",
"FileNameItems3WOOT.txt",
"FileNameItems4WOOT.txt",
}.AsQueryable();

var query =
 from      n in filesName
 group n by n.EndsWith("SUFFIX.txt") into ere
 select   new{ere} ;     

query.Dump();

The condition in the GROUP is not good. I am thinking to try to get all possible suffixe with a nested SELECT in the group but I can't find a way to do it.

How can I have 3 differents group, grouping by their suffixe with Linq? Is it possible?

*Jimmy answer is great but still doesn't work the way desired. Any fix?

+3  A: 

group by the suffix rather than whether it matches any particular one.

...
group by GetSuffix(n) into ere 
...

string GetSuffix(string n) {
   return Regex.Replace(n,"^Manifest|^FileNameItems[0-9]+", "");
}
Jimmy
Thanks, I had to modify the regex for the concrete code cause this example was very simple. I didn't knew that we can call method inside Linq. :P thanks!
Daok