我在SQL Server中创建了一个存储过程:
CREATE PROCEDURE PRODUCT_PROCEDURE
@USERID VARCHAR(MAX)
AS
BEGIN
SELECT distinct datawarehouse.dbo.orderformdump.itemno, basedescription,info,upc,CAST((SELECT [UNITPRICE] FROM PPPLTD.dbo.[ICPRICP] WHERE [ITEMNO] = replace([DataWarehouse].[dbo].[ORDERFORMDUMP].[ITEMNO],'-','') AND [PRICELIST] = (select top 1 priclist from PPPLTD.dbo.ARCUS where IDCUST = (select top 1 CUSTID from PPPLTD.dbo.WEBLOGINACCESS where [USER] = @USERID)) and [CURRENCY] = 'CDN' and DPRICETYPE = 1) AS DECIMAL(18,2))as price,caseqty, qty AS userquantity FROM [DataWarehouse].[dbo].[ORDERFORMDUMP] LEFT JOIN pppltd.dbo.weboeordd ON pppltd.dbo.WEBOEORDD.ITEMNO = REPLACE(datawarehouse.dbo.ORDERFORMDUMP.ITEMNO,'-','') and orduniq not in (select orduniq from pppltd.dbo.weboeordsubmit) and WEBOEORDD.ORDUNIQ in (select orduniq from pppltd.dbo.weboeordh where [user] = @USERID) LEFT JOIN DATAWAREHOUSE.dbo.webiteminfo on webiteminfo.itemno = orderformdump.itemno where (allowinbc = 'Yes' or allowinab = 'Yes') order by BASEDESCRIPTION
END
我已使用Entity Framework的Database First方法将该过程导入到我的项目中。它在DbContext中创建了一个模型和方法,为我执行存储过程。
型号:
public partial class PRODUCT_PROCEDURE_Result
{
public string itemno { get; set; }
public string basedescription { get; set; }
public string info { get; set; }
public string upc { get; set; }
public Nullable<decimal> price { get; set; }
public Nullable<int> caseqty { get; set; }
public Nullable<int> userquantity { get; set; }
}
DbContext方法:
public virtual ObjectResult<PRODUCT_PROCEDURE_Result> PRODUCT_PROCEDURE(string USERID)
{
var USERIDParameter = USERID != null ?
new ObjectParameter("USERID", USERID) :
new ObjectParameter("USERID", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<PRODUCT_PROCEDURE_Result>("PRODUCT_PROCEDURE", USERIDParameter);
}
我的问题是,当我单独执行var products = db.PRODUCT_PROCEDURE(username).ToList()
时,它会返回结果,但是当我向其添加where子句时,
var products = db.PRODUCT_PROCEDURE(username)
.Where
(item => item.basedescription.Contains(searchword)
|| item.info.Contains(searchword)
|| item.itemno.Contains(searchword)
|| item.itemno.Contains(searchword.Replace("-", ""))
|| item.upc.Contains(searchword)).ToList();
它返回null并抛出NullReferenceException?
P.S。我已检查过searchword
变量,但它包含一个值,因此不是问题。
答案 0 :(得分:1)
如果您的记录包含项目的某些字段的NULL值,那么我建议您使用以下修改:
var products = db.PRODUCT_PROCEDURE(username)
.Where
(item => (item.basedescription ?? "").Contains(searchword)
|| (item.info ?? "").Contains(searchword)
|| (item.itemno ?? "").Contains(searchword)
|| (item.itemno ?? "").Contains(searchword.Replace("-", ""))
|| (item.upc ?? "").Contains(searchword)).ToList();