views:

300

answers:

3

I have the follow Linq query ... which executes correctly:

from t in Tasks
where LookupTaskStarted(t.TaskId) == true
select new
{
     t.TaskId,
     t.Number,
     Started = LookupTaskStarted(t.TaskId)
}

Is there anyway to create this as a property on the Linq-To-Sql class? Or do I always have to reference it like this?

A: 

All linq-sql classes are created as partial. You could extend and add this property.

Perpetualcoder
If I did that ... and added this as a property would it fire this query every time I did a select? Not sure if I would want that kind of overhead.
mattruma
+1  A: 

I don't have the answer to your question, but I have a refactoring suggestion. Instead of calling LookupTaskStarted() twice, you can record the value with a let clause:

from t in Tasks
let started = LookupTaskStarted(t.TaskId)
where started
select new
{
    T.TaskId,
    t.Number,
    Started = started
}

After writing that, I realized that if you are filtering by started, you don't need the Started property because all of them will be true.

Bryan Watts
+1  A: 
class MyTask
{
   public int TaskId {get; set;}
   public int Number {get; set;}
   public bool Started {get; set;}
   public MyTask(Task t)
   {
       TaskId = t.TaskId;
       Number = t.Number;
       Started = LookupTaskStarted(t.TaskId)
   }
}
// :
// :
from t in Tasks
where LookupTaskStarted(t.TaskId) == true
select new MyTask(t);
James Curran