在Linq查询中使用自定义函数

时间:2010-11-12 07:31:37

标签: linq linq-to-sql

我在我的asp.net mvc应用程序中使用L2S进行数据库操作我在我的存储库中执行了简单查询

(from pt in db.oaProjectTasks
                   where pt.ProjectID == ProjectID
                   join t in db.oaTasks on pt.TaskID equals t.TaskID
                   where t.ParentTaskID == null
                   let daypassed = GetDaysPassed(t.StartDate,t.Duration)
                   select new ChartTask{TaskNumber = t.TaskNumber,StartDate = t.StartDate,
                   DurationRemaining = t.Duration - daypassed,TaskDescription = t.Task, DaysPassed = daypassed,Duration = t.Duration }).ToList();
下面的

是GetDayPassed方法的定义

private int GetDaysPassed(DateTime StartDate, int Duration) 
        {
            int retVal;
            if ((DateTime.Now - StartDate).Days > 0)
            {
                if ((DateTime.Now - StartDate.AddDays(Duration)).Days > 0)
                {
                    retVal = Duration;
                }
                else
                {
                    retVal = (DateTime.Now - StartDate).Days;
                }
            }
            else 
            {
                retVal = 0;
            }
            return retVal;
        }

没有编译时错误,但是,当我执行代码时,它给出了带有以下消息的invalidOperationException。

Could not translate expression 'Table(oaProjectTask).Where(pt => (pt.ProjectID == Invoke(value(System.Func`1[System.Int64])))).Join(Table(oaTask), pt => pt.TaskID, t => t.TaskID, (pt, t) => new <>f__AnonymousType5f`2(pt = pt, t = t)).Where(<>h__TransparentIdentifier2 => (<>h__TransparentIdentifier2.t.ParentTaskID == null)).Select(<>h__TransparentIdentifier2 => new 

我该如何解决这个问题?如果不允许在查询中调用方法,如何在Linq查询中进行简单计算而不是调用GetDayPassed方法?

1 个答案:

答案 0 :(得分:5)

你可以试试这个:

(from pt in db.oaProjectTasks
 where pt.ProjectID == ProjectID
 join t in db.oaTasks on pt.TaskID equals t.TaskID
 where t.ParentTaskID == null
 select t)
    .ToList() // T-SQL query will be executed here and result will be returned
    .Select(t => new ChartTask {
       TaskNumber = t.TaskNumber,
       StartDate = t.StartDate,
       DurationRemaining = t.Duration - GetDaysPassed(t.StartDate,t.Duration),
       TaskDescription = t.Task, 
       DaysPassed = GetDaysPassed(t.StartDate,t.Duration),
       Duration = t.Duration });

问题是Linq to Sql尝试将您的自定义函数转换为T-SQL,因为它不知道如何执行此操作会抛出异常。在我的例子中,Linq将构造查询,执行它(在调用.ToList()之后)并且您的函数将被称为Linq到对象查询。