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 - [email protected]
http://lists.ximian.com/mailman/listinfo/mono-list