views:

177

answers:

1

This is a follow up from here -->multiple-sorting-on-linq-nested-method .

Basically, on let memberName = ... it is throwing this exception Method 'System.String MemberName(Int32)' has no supported translation to SQL. and I am not figuring out the solution.

Also, BLLCmo and BLLConnect actually use TWO different DataBases. The original app(not mine) uses 4 Seperate DB's so I am trying to make due.


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();
    }
+2  A: 

The problem is that LINQ to SQL is trying to translate your method into SQL. Since MemberName isn't valid SQL, it gives up. Instead, you'll need to pull down the data you need from SQL and then call your methods (and sort) in a separate LINQ to Objects query:

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();
}
dahlbyk
This is one of those solutions that are so simple and so nice that I feel stupid for not getting there on my own. It works great!
Refracted Paladin