[Mono-list] Can this linq be optimised?

Andy York andy at brdstudio.net
Wed Jun 5 16:15:49 UTC 2013


Is this running against a database? If so you can always get the SQL 
that is generated by the LINQ statement and check it running the SQL 
statement directly against the database and checking the number of reads 
and the time. The fact that your using a distinct hints that you may be 
missing a part of the query.

Just a suggestion.

On 6/5/2013 11:15 AM, Stifu wrote:
> 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.
> _______________________________________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list
>
>
> -----
> No virus found in this message.
> Checked by AVG - www.avg.com
> Version: 2013.0.3343 / Virus Database: 3184/6384 - Release Date: 06/04/13
>
>





More information about the Mono-list mailing list