Nested LINQ method throwing "Not supported ..."
This is a continuation from here -> multiple-sorting-on-linq-nested-method .
Basically, let memberName = ...
it throws this exception Method 'System.String MemberName(Int32)' has no supported translation to SQL.
and I don't understand the solution.
Besides, BLLCmo
and BLLConnect
use TWO various databases. The original application (not mine) uses 4 Separate DB, so I am trying to do this.
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();
}
source to share
The problem is that LINQ to SQL is trying to translate your method into SQL. Since the MemberName
SQL is invalid, it refuses. Instead, you will need to pull the data you want from the 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();
}
source to share