简体   繁体   中英

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.

Expression lambda has following syntax param => expression . Ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM