[Mono-list] Can this linq be optimised?

Rolf Bjarne Kvinge rolf at xamarin.com
Thu Jun 6 09:21:28 UTC 2013


Hi,

If you have lots of duplicates it _might_ be faster to pass a custom
comparison function to Distinct() and then fixup the resulting strings in
the tmp list afterwards instead.

Another thing you might want to try is to check if the string actually
needs modification instead of always creating substrings and then
concatenating them, even if the result would be identical to the input.

Rolf

On Wed, Jun 5, 2013 at 1:32 AM, Paul Johnson <paul at all-the-johnsons.co.uk>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 lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20130606/f9a91c17/attachment-0001.html>


More information about the Mono-list mailing list