多个where代码中的条件只导致第一行显示,
public bill_transaction Getbill_transaction(int id)
{
bill_transaction bill_transaction = db.bill_transaction.Where(m => m.Cust_Id == id && m.Bill_Status == "P").FirstOrDefault();
if (bill_transaction == null)
{
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return bill_transaction;
}
我想显示符合此条件的所有行
即使对于单一条件也是如此,第一行只是显示,我怎么能显示所有行。
答案 0 :(得分:0)
只需删除FirstOrDefault
,这也就是执行下一次检查bill_transaction == null
的原因。所以看起来你也想删除那个检查,返回类型也不仅仅是bill_transaction
,它应该是IQueryable<bill_transaction>
或IEnumerable<bill_transaction>
。总结一下,我们有这个简单的新方法:
public IQueryable<bill_transaction> Getbill_transactions(int id)
{
var ts = db.bill_transaction.Where(m => m.Cust_Id == id && m.Bill_Status == "P");
if (!ts.Any()) {
throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotFound));
}
return ts;
}
注意,如果没有任何事务,则抛出异常(此行为取决于您)。