MVC无效的长度参数传递给LEFT或SUBSTRING函数

时间:2018-08-10 07:00:10

标签: c# linq

我有一条SQL命令,其中我试图在一个表中联接多个列,

 dbo.tbl_Application AS APP on (APP.motor_no = SUBSTRING(R.remarks, CHARINDEX('|', R.remarks) + 1, LEN(R.remarks))) AND (Year(APP.date_created) = YEAR(R.txndate)) AND (APP.motor_no = M.motor_no)

现在我将其转换为Linq

已转换为LINQ

join APP in db.tbl_Application on new 
 { motor_no = R.remarks.Substring(5), R.txndate.Value.Year, motor_no2 = M.motor_no }
 equals new 
 { motor_no = APP.motor_no, APP.date_created.Value.Year, motor_no2 = APP.motor_no }

然后添加到我现有的linq中:

现有LINQ

from R in db.vwEtracs_Receipt
join RI in db.vwEtracs_ReceiptItem on new { parentid = R.objid } equals new { parentid = RI.parentid }
join IA in db.vwEtracs_IncomeAccount on new { objid = RI.acctid } equals new { objid = IA.objid }
join M in db.vwQVFS_Motor on new { motor_no = R.remarks.Substring(5) } equals new { motor_no = M.motor_no } into M_join
from M in M_join.DefaultIfEmpty()
join OOI in db.vwQVFS_OOI on new { operator_id = R.payerId } equals new { operator_id = OOI.operator_id } into OOI_join
from OOI in OOI_join.DefaultIfEmpty()
join CR in db.vwQVFS_COR on M.motor_id equals CR.motor_id into CR_join
from CR in CR_join.DefaultIfEmpty()
join F in db.vwQVFS_Franchise on new { or_id = R.objid } equals new { or_id = F.or_id } into F_join
from F in F_join.DefaultIfEmpty()
join T in db.vwQVFS_TODA on M.toda_id equals T.toda_id into T_join
from T in T_join.DefaultIfEmpty()
join RT in db.vwQVFS_RT on new { payer_id = R.payerId } equals new { payer_id = RT.payer_id } into RT_join
from RT in RT_join.DefaultIfEmpty()
join B in db.vwQVFS_Brand on M.brand_id equals B.brand_id into B_join
from B in B_join.DefaultIfEmpty()

如果我从上面添加代码行,则会收到错误Invalid length parameter passed to the LEFT or SUBSTRING function.,但是如果删除它,我的Linq可以正常工作。

是什么原因导致此错误?预先感谢。

1 个答案:

答案 0 :(得分:0)

您收到此错误,是因为数据库db.vwEtracs_Receipt.remarks列中至少有一条记录少于5个字符。因此,在您的SQL中,实际上并没有传递长度,但是在linq中,您使用了Substring(int length)。因此,子字符串的长度不能超过实际的字符串长度