danarj
danarj

Reputation: 1838

Entity framework where clause

I am newbie to EF how to use and - or in where clause in the entity framework

HR_ATTENDANCE_DEVICE_SHUTDOWN_TBL attendanceDeviceShutdownTbl = 
    context.HR_ATTENDANCE_DEVICE_SHUTDOWN_TBL
        .FirstOrDefault(x => x.Device_ID.Equals(model.DeviceId) &&
                             x=>x.Device_Name=model.DeviceName);

the above code will not work but how can I make it works.

Upvotes: 4

Views: 4986

Answers (1)

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236208

Expression lambda has following syntax param => expression. I.e. its like simple method, which have input parameter(s) and body. You define parameters only once and then use them in body of method or lambda:

HR_ATTENDANCE_DEVICE_SHUTDOWN_TBL attendanceDeviceShutdownTbl = 
    context.HR_ATTENDANCE_DEVICE_SHUTDOWN_TBL.FirstOrDefault(x => 
        x.Device_ID.Equals(model.DeviceId) && x.Device_Name == model.DeviceName);

In this case you have single parameter x which goes to anonymous function body. Body is an expression which should return boolean value and (usually) use parameter x. In your case lambda body should be

x.Device_ID.Equals(model.DeviceId) && x.Device_Name == model.DeviceName

Suggested reading: Lambda Expressions (C# Programming Guide). Also note == is a comparison operator. = is an assignment operator. Do not mix up with them.

Upvotes: 5

Related Questions