[Mono-list] Optimising some large loops using LINQ question

Alberto León leontiscar at gmail.com
Wed Apr 24 08:58:01 UTC 2013


I suggest to store the result of db.getSubscription().Tasks in an
IEnumerable<Task> or List<Task>
Is possible that each time you pass for the condition in the loop you
called anothertime getSuscription()
So you are calling many times a high resources consumption task, that is a
database action.


2013/4/24 David Schmitt <david at dasz.at>

> On 24.04.2013 02:49, Paul Johnson wrote:
>
>> Hi,
>>
>> I'm working on a project that has some very large loops in it that I'd
>> like to remove and use some bits of LINQ to speed things up.
>>
>> The current code looks like this
>>
>> foreach (Task t in db.getSubscription().Tasks)
>> {
>>       if (t.TaskID == task.TaskID)
>>       {
>>            foreach (ReadTask rt in db.ReadTasks.ReadTasks)
>>            {
>>                if (rt.TaskID == task.TaskID)
>>                {
>>                     result = true;
>>                     break;
>>                }
>>            }
>>        }
>> }
>>
>> db.getSubscription().Tasks and db.ReadTasks.ReadTasks can both be huge,
>> so iterating through them could take quite a while.
>>
>> The LINQ I've come up with looks like this
>>
>> Task t = db.getSubscription().Tasks.**FirstOrDefault(s => s.TaskID ==
>> task.TaskID);
>> if (t != null)
>> {
>>       ReadTask rt = db.ReadTasks.ReadTasks.**FirstOrDefault(s => s.TaskID
>> == task.TaskID);
>>       if (rt != null)
>>              result = true;
>> }
>>
>> Questions are, are these two pieces of code equivalent and should this
>> be FirstOrDefault or just First?
>>
>
> Without knowing what db does internally, your code looks like it could be
> simplified to this:
>
> static bool GetTask(Task task) {
>         return db.ReadTasks.ReadTasks.Any(rt => rt.TaskID == task.TaskID)
> }
>
> Assuming that there is a hidden connection between
> db.getSubscription().Tasks and db.ReadTasks.ReadTasks, using LINQ might not
> be a great idea because streaming (or not) within the LINQ methods might
> screw up your call flow.
>
>
>
> Regards, David
>
> ______________________________**_________________
> Mono-list maillist  -  Mono-list at lists.ximian.com
> http://lists.ximian.com/**mailman/listinfo/mono-list<http://lists.ximian.com/mailman/listinfo/mono-list>
>



-- 
https://twitter.com/AlbertCSharpMan
http://stackoverflow.com/users/690958/alberto-leon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.ximian.com/pipermail/mono-list/attachments/20130424/381ed88e/attachment.html>


More information about the Mono-list mailing list