本文介绍了将INT作为null传递给搜索函数(使用LINQ的EF)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在构建搜索功能(fname,lname,ChkNum,zip)。 INT中的ChkNum。我无法将NULL传递给INT,因为用户可能没有输入ChkNum进行搜索。 黑客代码在下面..它正在工作,但我确信有更好的方法。 var result = ( from c in ctx.OfferOrdersDetails 其中 c.custFirstName.StartsWith(userFName) && c.custLastName.StartsWith(userLName) orderby c.custLastName 选择 c ); if (chkNum.HasValue){ result = result.Where(c => c.oOrderCheckNo == chkNum .value的).OrderBy(C => c.custLastName); } if (!String.IsNullOrWhiteSpace(custZIP)) result = result.Where(c = > c.custZIP.StartsWith(custZIP))。OrderBy(c => c.custLastName); return result.ToList(); } 解决方案 LINQ在此阶段,实体不处理可为空的整数或IsNullOrWhiteSpace。您仍然可以在一行中编写查询,如下所示: var result = (来自c in ctx .OfferOrdersDetails 其中c.custFirstName.StartsWith(userFName)&& c.custLastName.StartsWith(userLName)&&(!chkNum.HasValue || c.oOrderCheckNo = = chkNum.Value)&&(custZIP == null || custZIP == String.Empty || c.custZIP.StartsWith(custZIP)) orderby c.custLastName select c ); ~Rowan I am building a search functions (fname,lname,ChkNum,zip) . ChkNum in INT. I can not pass a NULL into INT as the user might not enter a ChkNum for searching.The hack code is below .. its working, but I am sure there is a better way. var result = (from c in ctx.OfferOrdersDetails where c.custFirstName.StartsWith(userFName) && c.custLastName.StartsWith(userLName) orderby c.custLastName select c );if(chkNum.HasValue) { result = result.Where(c => c.oOrderCheckNo == chkNum.Value).OrderBy(c=>c.custLastName);}if (!String.IsNullOrWhiteSpace(custZIP)) result = result.Where(c => c.custZIP.StartsWith(custZIP)).OrderBy(c=>c.custLastName);return result.ToList();} 解决方案 Hi,LINQ to Entities doesn't handle nullable integers or IsNullOrWhiteSpace at this stage. You can still write the query in a single line as follows:var result = (from c in ctx.OfferOrdersDetails where c.custFirstName.StartsWith(userFName) && c.custLastName.StartsWith(userLName) && (!chkNum.HasValue || c.oOrderCheckNo == chkNum.Value) && (custZIP == null || custZIP == String.Empty || c.custZIP.StartsWith(custZIP)) orderby c.custLastName select c ); ~Rowan 这篇关于将INT作为null传递给搜索函数(使用LINQ的EF)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-21 13:31