[Mono-list] Can this linq be optimised?

Paul Johnson paul at all-the-johnsons.co.uk
Tue Jun 4 23:32:04 UTC 2013


I have a class containing ints, strings and anything else you'd expect 
to find in a class. I create a list of the class and then extract the 
strings and perform a Distinct() on them. That bit is easy.

Problem is this - the strings all have something in brackets. Sometimes 
the braces have a space on the inside before and after the single word 
inside of them and sometimes they don't.

My LINQ query looks like this

  List<string> tmp = (from m in cropTypes
                                   let cw = m.CropName
                                   let kw = cw.Substring(0, 
cw.LastIndexOf(")") + 1)
                                 let t = kw.IndexOf('(')
                                 let p1 = kw.Substring(0, t)
                                 let p2 = kw.Substring(t, (kw.Length - 
1) - t + 1).Replace(" ", string.Empty)
                                 let p3 = p1 + p2
                                   select p3).Distinct().ToList();

This query works fine but looks inefficient. Is there a way to rewrite 
this to remove any spaces inside of the braces?


"Space," it says, "is big. Really big. You just won't believe how 
vastly, hugely, mindbogglingly big it is. I mean, you may think it's a 
long way down the road to the chemist's, but that's just peanuts to 
space, listen..."
Hitch Hikers Guide to the Galaxy, a truly remarkable book!

More information about the Mono-list mailing list