var item1 = (from a in db.Item
where
(a.ItemNo == Item.ItemNo || Item.ItemNo==null ) &&
(a.StatusId == Item.StatusId || Item.StatusId == 0) &&
(a.LocationId == Item.LocationId || Item.LocationId == 0)
select a).ToList();
return View(item1);
我正在异常
[ Argument # = 1,Name of function(if known) = isnull ]
我也试过这段代码
var item1 = (from a in db.Item
where
(a.ItemNo == Item.ItemNo || Item.ItemNo=="") &&
(a.StatusId == Item.StatusId || Item.StatusId == 0) &&
(a.LocationId == Item.LocationId || Item.LocationId == 0)
select a).ToList();
return View(item1);
但无法解决错误
[String truncation: max=0, len=2, value='10'. ]
答案 0 :(得分:1)
您可以尝试将其编写为Lamdba表达式,看看它是否是同一个问题?
var item1 = db.Item.Where(a => (String.IsNullOrEmpty(Item.ItemNo) || a.ItemNo == Item.ItemNo) &&
(Item.StatusId == 0 || a.StatusId == Item.StatusId) &&
(Item.LocationId == 0 || a.LocationId == Item.LocationId)).FirstOrDefault();
return View(item1);
<强>更新强>
出现EF不支持String.IsNullOrEmpty
请参阅this question。您可以重写您的查询,如:
var item1 = db.Item.Where(a => ((Item.ItemNo == null || Item.ItemNo == String.Empty) || a.ItemNo == Item.ItemNo) &&
(Item.StatusId == 0 || a.StatusId == Item.StatusId) &&
(Item.LocationId == 0 || a.LocationId == Item.LocationId))
.FirstOrDefault();
如果您仍然无法检查null,那么您可以打破查询,例如
var query = db.Item.Where(a => (Item.StatusId == 0 || a.StatusId == Item.StatusId) &&
(Item.LocationId == 0 || a.LocationId == Item.LocationId));
if (!String.IsNullOrEmpty(Item.ItemNo))
{
query = query.Where(a => a.ItemNo == Item.ItemNo);
}
var item1 = query.FirstOrDefault();
答案 1 :(得分:0)
修改强> 试试这样的事情
string.IsNullOrEmpty(Item.ItemNo) ? a.ItemNo==a.ItemNo
: a.ItemNo == Item.ItemNo;
你的代码是
var item1 = (from a in db.Item
where
( string.IsNullOrEmpty(Item.ItemNo) ? a.ItemNo==a.ItemNo
: a.ItemNo == Item.ItemNo; ) &&
..more
注意如果Item.ItemNo
的值为空或空,那么a.ItemNo==a.ItemNo
它应该满足您的条件......
确保您的itmeno字段可以为空,如下所示
int? ItemNo { get; set; }
并尝试像这样的东西
Item.ItemNo == null ? a.ItemNo : a.ItemNo == Item.ItemNo
所以它会
var item1 = (from a in db.Item
where
( Item.ItemNo == null ? a.ItemNo : a.ItemNo == Item.ItemNo ) &&
..more