[Mono-list] Can this linq be optimised?

Stifu stifu at free.fr
Wed Jun 5 15:15:12 UTC 2013


A single Substring() (to isolate the part between brackets), then a Trim()?
Or if the word between the brackets is not supposed to contain ANY space,
maybe use the Trim overload that expects a char array parameter.

Like this (except in your LINQ query):
string word = "(  hello    )";
char[] trimChars = new char[] {'(', ' ', ')'};
string result = word.Trim(trimChars);

PS: if it works and is fast enough, then maybe it's good enough. It may look
(and actually be) inefficient, but if it doesn't affect users, don't bother
unless you've got time to kill. You might also be interested in double
checking Regex performances, but I'd expect it to be slower.


nodoid wrote
> Hi,
> 
> 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?
> 
> Thanks
> 
> Paul
> -- 
> "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!
> 
> _______________________________________________
> Mono-list maillist  -  

> Mono-list at .ximian

> http://lists.ximian.com/mailman/listinfo/mono-list





--
View this message in context: http://mono.1490590.n4.nabble.com/Can-this-linq-be-optimised-tp4659853p4659859.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list