Add user-defined scalar function as property of Linq-To-Sql class

I have the following Linq query ... which is running correctly:

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

      

Can this property be created as a property of the Linq-To-Sql class? Or do I always need to refer to it like this?

0


source to share


3 answers


I don't have an answer to your question, but I have a refactoring suggestion. Instead of calling LookupTaskStarted () twice, you can write the value using a statement let

:

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

      



After that, I realized that if you are filtering started

, you don't need the property started

, because they will all be true.

+1


source


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);

      



+1


source


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

0


source







All Articles