我有一个方法,我用条件基础更改了结束逻辑,如下所示,从那时起它开始抛出异常
public async Task<IEnumerable<CorpTransaction_DTO>> GetTransactionDetailByCorpNumber(string corpNumber)
{
var query = from rm in _gpModel.RM20101
join cm in _gpModel.RM00101.AsNoTracking() on rm.CUSTNMBR equals cm.CUSTNMBR
join rm1 in _gpModel.RM20201 on rm.DOCNUMBR equals rm1.APTODCNM into rm1Join
from rm1 in rm1Join.DefaultIfEmpty()
join so in _gpModel.SOP30200.AsNoTracking() on rm.DOCNUMBR equals so.SOPNUMBE into soJoin
from sop in soJoin.DefaultIfEmpty()
join sy in _gpModel.SY03900.AsNoTracking() on sop.NOTEINDX equals sy.NOTEINDX into syJoin
from sy in syJoin.DefaultIfEmpty()
**where
new string(rm.SLPRSNID.Where(char.IsDigit).ToArray()) == corpNumber ||
(
rm.SLPRSNID.EndsWith(corpNumber) &&
!ApplicationConstants.ExcludedPrefixes.Any(prefix => rm.SLPRSNID.StartsWith(prefix))
)**
orderby rm.DOCDATE descending
select new CorpTransaction_DTO
{
DOCDATE = rm.DOCDATE,
RMDTYPAL = rm.RMDTYPAL,
AgentId = cm.CUSTNMBR.Trim(),
AgentName = cm.CUSTNAME.Trim(),
Batch = rm.BACHNUMB.Trim(),
InvoiceNumber = rm.DOCNUMBR.Trim(),
DUEDATE = rm.DUEDATE,
InvoiceType = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_CASH_RECEIPT ? ApplicationConstants.INVOICETYPE_PAYMENT :
rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_SALES_INVOICE ? ApplicationConstants.INVOICETYPE_INVOICE :
rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_DEBIT_MEMO || rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_CREDIT_MEMO
|| rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_Return ? ApplicationConstants.INVOICETYPE_ADJ : string.Empty,
InvoiceStatus = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_SALES_INVOICE ? (rm.CURTRXAM == 0 ?
ApplicationConstants.INVOICESTATUS_PAID : rm.CURTRXAM < rm.ORTRXAMT ? ApplicationConstants.INVOICESTATUS_PARTIALLY_PAID : ApplicationConstants.INVOICESTATUS_UNPAID)
: null,
Amount = rm.ORTRXAMT,
Balance = rm.CURTRXAM,
SubType = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_CASH_RECEIPT || rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_DEBIT_MEMO ? string.Empty :
rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_CREDIT_MEMO ? ApplicationConstants.INV_SUB_TYPE_DISCOUNT : rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_Return ? ApplicationConstants.INV_SUB_TYPE_RETRUN : string.Empty,
InvoiceDescription = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_SALES_INVOICE && sy != null ? sy.TXTFIELD ?? string.Empty : string.Empty,
RequestedBy = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_SALES_INVOICE && sy != null ? sy.TXTFIELD ?? string.Empty : string.Empty,
SourceSystem = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_SALES_INVOICE && sy != null ? sy.TXTFIELD ?? string.Empty : string.Empty,
FormattedInvoiceNumber = rm.RMDTYPAL == ApplicationConstants.RMDTYPAL_SALES_INVOICE ? rm.SLPRSNID.Trim() + " - " + rm.DOCNUMBR.Trim() : string.Empty
};
// Execute the query and get the list of DTOs
var corpDto = await query.ToListAsync();
// Apply transformations to the list of DTOs
corpDto.ForEach(account => account.SubType = account.RMDTYPAL == 1 ? GPHelper.SplitStringToColumns(account.InvoiceDescription).InvoiceDescription : account.SubType);
corpDto.ForEach(account => account.InvoiceDescription = GPHelper.SplitStringToColumns(account.InvoiceDescription).InvoiceDescription);
corpDto.ForEach(account => account.RequestedBy = GPHelper.SplitStringToColumns(account.RequestedBy).RequestedBy);
corpDto.ForEach(account => account.SourceSystem = GPHelper.SplitStringToColumns(account.SourceSystem).SourceSystem);
// Group by DOCNUMBR and select the first entry from each group
corpDto = corpDto.GroupBy(account => account.DOCNUMBR).Select(group => group.First()).ToList();
return corpDto;
}
如果我消除这个条件,效果很好
where new string(rm.SLPRSNID.Where(char.IsDigit).ToArray()) == corpNumber ||
(
rm.SLPRSNID.EndsWith(corpNumber) &&
!ApplicationConstants.ExcludedPrefixes.Any(prefix => rm.SLPRSNID.StartsWith(prefix))
)
这是我的模型
public class CorpTransaction_DTO
{
public DateTime DOCDATE { get; set; }
public string Batch { get; set; }
public string InvoiceNumber { get; set; }
public string DOCNUMBR { get; set; }
public DateTime DUEDATE { get; set; }
public string InvoiceType { get; set; }
public string InvoiceStatus { get; set; }
public decimal Amount { get; set; }
public decimal Balance { get; set; }
public string CustomerPayment { get; set; }
public string SubType { get; set; }
public string PaymentDescription { get; set; }
public string AgentId { get; set; }
public string AgentName { get; set; }
public string InvoiceDescription { get; set; }
public string FormattedInvoiceNumber { get; set; }
public string RequestedBy { get; set; }
public string SourceSystem { get; set; }
public int RMDTYPAL { get; set; }
}
您无法调用字符串构造函数从 linq 查询中的数组创建字符串,因为它没有 sql 翻译。为了实现相同的逻辑,您可以考虑将列的数字部分存储在单独的列中,以便将该列直接与 corpnumber 变量进行比较。这也将为您带来更好的性能。