Multiple where condition in ntt framework mvc

160 Views Asked by At

Multiple where condition in following code result in only first row displaying,

     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;
    }

i want to display all rows matching with this condition,

even for single condition also, first row only displying, how i can display all rows.

1

There are 1 best solutions below

0
On

Just remove FirstOrDefault, also that's why the next check bill_transaction == null is performed. So looks like you also want to remove that check, the return type is also not just bill_transaction, it should be IQueryable<bill_transaction> or IEnumerable<bill_transaction>. Sum up all we have this simple new method:

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;
}

Note if there is not any transaction, an exception is thrown (this behavior is up to you).