This statement can't be directly translated to any equivalent SQL commands:
var patients = allpatients.Select(p => CreatePatient(p));
Common way to use custom extension methods in LINQ to Entities (EF data context) is perform query on the model first (which able to be translated into SQL statements), then use custom extension method outside query context (this is just an example):
var patients = allpatients.Select(p => new Patient()
{
FIRSTNAME = p.FIRSTNAME,
MIDDLENAME = p.MIDDLENAME,
SURNAME = p.SURNAME
});
foreach (Patient pt in patients)
{
// iterate through Patient collection
// use your custom method here
}
Note that the same error also occurs if the custom method becomes part of the new
model assignment inside LINQ query like this example:
var patients = allpatients.Select(p => new Patient()
{
PATIENTID = ToInt32(p.PATIENTID), // ToInt32 is a custom extension method, e.g. converting string to int
FIRSTNAME = p.FIRSTNAME,
MIDDLENAME = p.MIDDLENAME,
SURNAME = p.SURNAME
});
This is the correct way of above usage:
var patients = allpatients.Select(p => new Patient()
{
PATIENTID = p.PATIENTID, // leave the selected property as-is
FIRSTNAME = p.FIRSTNAME,
MIDDLENAME = p.MIDDLENAME,
SURNAME = p.SURNAME
});
// another method content
foreach (Patient pt in patients)
{
other.PATIENTID = ToInt32(pt.PATIENTID);
}
Reference:
LINQ to Entities does not recognize the method