简体   繁体   English

当在LINQ中添加子句时,存储过程结果返回Null

[英]Stored Procedure Results Return Null When Where Clause is Added in LINQ

I've created a stored procedure in SQL Server: 我在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

and I've imported the procedure into my project using Entity Framework's Database First approach. 并且我已使用Entity Framework的数据库优先方法将过程导入到我的项目中。 It's created a model and a method inside the DbContext that executes the stored procedure for me. 它在DbContext中创建了一个模型和一个方法,为我执行存储过程。

Model: 模型:

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 Method: 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);
        }

My question is how come when I execute var products = db.PRODUCT_PROCEDURE(username).ToList() alone, it returns results, but when I add a where clause to it, 我的问题是,当我单独执行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();

it returns null and throws a NullReferenceException? 它返回null并引发NullReferenceException吗?

PS I've checked the searchword variable and it contains a value, therefore that's not the issue. PS我已经检查了searchword变量,它包含一个值,因此这不是问题。

If you records contain NULL values for some of the fields of the items then I'd suggest to use the following modification: 如果您记录的某些项目字段包含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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM