我需要在linq中編寫一個自定義函數。如何在LINQ子句中編寫自定義函數
private short GetSignByEntityType(string entityType, InvoiceReportType invoiceReportType)
{
if (invoiceReportType == InvoiceReportType.WithholdingTaxReport)
{
if (entityType == "R") return 1;
if (entityType == "I") return -1;
}
return this._sign;
}
var headerDataWithBook1Rates = (
from hd in headerDataToUse
from vi in voucherItems.Where(x => x.VoucherID == hd.VoucherID && x.Sign == GetSignByEntityType(hd.EntityType, request.ReportType)).DefaultIfEmpty()
from dvi in draftVoucherItems.Where(x => x.DraftVoucherID == -hd.VoucherID && x.Sign == GetSignByEntityType(hd.EntityType, request.ReportType)).DefaultIfEmpty()
from vacc in accounts.Where(x => x.ID == vi.AccountID).DefaultIfEmpty()
from dvacc in accounts.Where(x => x.ID == dvi.AccountID).DefaultIfEmpty()
.....
它給我一個錯誤。
System.NotSupportedException:方法的Int16 GetSignByEntityType(System.String, Enka.Gfs.Domain.SharedEntities.Request.InvoiceReportType)「沒有 支持轉換爲SQL。
我該如何解決這個問題?
如果您使用的是純LINQ,但您使用的是EntityFramework/Linq-to-SQL,則可以這樣做。它試圖在SQL服務器上執行你的函數,這顯然不知道'GetSignByEntityType()'的意思。 – AndrewP