.net linq到实体“不在”单个查询

时间:2009-08-07 05:36:01

标签: .net linq

我使用linq实体,我希望得到所有未注册该类的用户。这就是我做的。

var classUsers = from cu in myEntities.ClassUsers
                         where cu.Class.ClassId == classId
                         select new
                         {
                             FirstName = cu.UserInfo.FirstName,
                             Id = cu.UserInfo.Id,
                             LastName = cu.UserInfo.LastName,
                             Select = new Boolean()
                         };

        var allUsers = from u in myEntities.UserInfo
                       select new
                       {
                           FirstName = u.FirstName,
                           Id = u.Id,
                           LastName = u.LastName,
                           Select = new Boolean()
                       };

        var availableUsers = allUsers.Except(classUsers).OrderBy(a=>a.FirstName);

有没有办法可以将前两个查询合并为一个?

1 个答案:

答案 0 :(得分:0)

我没有测试它,但这可能是一个轻量级的解决方案。

myEntities.UserInfo.Except(myEntities.ClassUsers.Where(cu=> cu.Class.ClassID = classID)
.Select(cu=>cu.UserInfo)).Select(u=>Id = u.Id, FirstName = u.FirstName, LastName =   
                                 u.LastName, Select = new Boolean())