嵌套LINQ方法抛出“不支持...”异常

时间:2009-10-07 20:59:09

标签: c# linq linq-to-sql exception let

这是从这里开始的跟进 - > multiple-sorting-on-linq-nested-method

基本上,在let memberName = ...上,它会抛出此异常Method 'System.String MemberName(Int32)' has no supported translation to SQL.并且我没有找到解决方案。

此外,BLLCmoBLLConnect实际上使用两个不同的数据库。原始应用程序(不是我的)使用4个单独的数据库,因此我正在尝试到期。


BLLCmo.cs

    public static DataTable GetAllMembers(Guid workerID)
    {
        var AllEnrollees = from enrollment in context.tblCMOEnrollments
                               where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
                               join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
                               from worker in workerGroup.DefaultIfEmpty()
                               let memberName = BLLConnect.MemberName(enrollment.ClientID)
                               orderby enrollment.DisenrollmentDate ascending, memberName ascending 
                               select new
                                       {
                                           enrollment.ClientID,
                                           MemberName = memberName,
                                           NurseName = BLLAspnetdb.NurseName(enrollment.CMONurseID),
                                           SocialWorkerName =BLLAspnetdb.SocialWorkerName(enrollment.CMOSocialWorkerID),
                                           enrollment.DisenrollmentDate,
                                           enrollment.EnrollmentDate,
                                           ESFirstName = worker.FirstName,
                                           ESLastName = worker.LastName,
                                           ESPhone = worker.Phone
                                       };

        var dataTable = AllEnrollees.CopyLinqToDataTable();

        return dataTable;
    }

BLLConnect.cs

    public static String MemberName(Int32 personID)
    {
        var memberName = from person in context.tblPersons
                         where person.PersonID == personID
                         select person.FirstName + " " + person.LastName;

        return memberName.SingleOrDefault();
    }

1 个答案:

答案 0 :(得分:4)

问题是LINQ to SQL正在尝试将您的方法转换为SQL。由于MemberName不是有效的SQL,它会放弃。相反,您需要从SQL下拉所需的数据,然后在单独的LINQ to Objects查询中调用您的方法(并排序):

public static DataTable GetAllMembers(Guid workerID)
{
    var AllEnrollees =
        from enrollment in context.tblCMOEnrollments
        where enrollment.CMOSocialWorkerID == workerID || enrollment.CMONurseID == workerID
        join supportWorker in context.tblSupportWorkers on enrollment.EconomicSupportWorkerID equals supportWorker.SupportWorkerID into workerGroup
        from worker in workerGroup.DefaultIfEmpty()
        select new
        {
            enrollment.ClientID,
            enrollment.CMONurseID,
            enrollment.CMOSocialWorkerID,
            enrollment.DisenrollmentDate,
            enrollment.EnrollmentDate,
            ESFirstName = worker.FirstName,
            ESLastName = worker.LastName,
            ESPhone = worker.Phone
        };

    var result =
        from enrollee in AllEnrollees.AsEnumerable()
        let memberName = BLLConnect.MemberName(enrollee.ClientID)
        orderby enrollee.DisenrollmentDate ascending, memberName ascending 
        select new
        {
            enrollee.ClientID,
            MemberName = memberName,
            NurseName = BLLAspnetdb.NurseName(enrollee.CMONurseID),
            SocialWorkerName = BLLAspnetdb.SocialWorkerName(enrollee.CMOSocialWorkerID),
            enrollee.DisenrollmentDate,
            enrollee.EnrollmentDate,
            enrollee.ESFirstName,
            enrollee.ESLastName,
            enrollee.ESPhone
        };

    return result.CopyLinqToDataTable();
}