Where和子句实体框架

时间:2013-09-27 13:32:29

标签: entity-framework

我会在多个条件下选择

我知道我可以选择使用这个陈述

    var session = (from us in context.UserSessions
               where us.UserId == dbUser.Id
               select us).FirstOrDefault();

但我想做类似的事情

    var session = (from us in context.UserSessions
               where us.UserId == dbUser.Id
               and us.somefield == somevalue
               select us).FirstOrDefault();

但是无法识别和关键字

2 个答案:

答案 0 :(得分:3)

不要让查询语法欺骗你:) - 它是C#,所以and仍然是&&

var session = (from us in context.UserSessions
           where us.UserId == dbUser.Id
           && us.somefield == somevalue
           select us).FirstOrDefault();

或者,这可以在Lambda语法中更简洁地表达(如果使用带有谓词的FirstOrDefault overload,则可以删除冗余的SelectWhere子句:

var session = context
       .UserSessions
       .FirstOrDefault(us => us.UserId == dbUser.Id && us.somefield == somevalue);

答案 1 :(得分:0)

var session = (from us in context.UserSessions
               where us.UserId == dbUser.Id
               && us.somefield == somevalue
               select us).FirstOrDefault();