bnil
bnil

Reputation: 1541

syntax error in simple LINQ statement

I am developing MVC app and I am using the LINQ in controller.

I am trying to get one rechord with below query, but its giving an error...

  Approval oAP = new Approval();
  oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));

Is there any wrong syntax ?

Got the answer

oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();

Upvotes: 0

Views: 89

Answers (2)

Sam
Sam

Reputation: 1366

Some remarks:

You should be able to drop the Where:

oAP = db.Approvals.FirstOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));

Personally, I try to avoid the First and FirstOrDefault functions, because if you know there is only one record and if you want to enforce this, you can use SingleOrDefault:

oAP = db.Approvals.SingleOrDefault(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount));

If you know there will always be (more than) one record, you can drop the 'OrDefault' part and use First() or Single().

Upvotes: 0

Jorge
Jorge

Reputation: 18237

Change this

e.ApprovedBy.Id = loggedEmployee.Id

For

e.ApprovedBy.Id == loggedEmployee.Id

You're comparing not assigning values.

Also you may add this

oAP = db.Approvals.Where(e => (e.ApprovedBy.Id == loggedEmployee.Id) && (e.ReviewNo == oPaymentAdvice.ReviewCount)).FirstOrDefault();

Because i'm assuming that you want to return only one

Upvotes: 4

Related Questions