自定义Linq到odata的实体查询

时间:2015-09-29 07:19:08

标签: c# linq odata

我的实体类 Core_User 中有一个部分成员 IsSystemAdmin ,因此我必须选择新对象。我正在使用Odata,为此我必须先将查询转换为 AsEnumerable ,然后 AsQueryable ,以便它可以使用paging.Can我自定义以下linq查询。任何帮助将受到高度赞赏

[HttpGet]
[ODataRoute("GetUsersWithRole")]
public IQueryable<Core_User> GetUsersWithRole()
{
    var result = (from user in Db.Core_User
                  let newRole = (from userRole in Db.Auth_UserRole
                                 join role in Db.Auth_Role on userRole.RoleId equals role.Id
                                 where userRole.UserId == user.Id && role.Name == ApplicationRoleTypes.SystemAdmin
                                 select role
                          ).FirstOrDefault()
                  where user.UserType == (int)Enumerations.UserType.Agent && !user.IsDeleted
                  select new
                  {
                      user.Id,
                      user.Email,
                      user.EmailConfirmed,
                      user.PasswordHash,
                      user.SecurityStamp,
                      user.PhoneNumber,
                      user.PhoneNumberConfirmed,
                      user.TwoFactorEnabled,
                      user.LockoutEndDateUtc,
                      user.LockoutEnabled,
                      user.AccessFailedCount,
                      user.UserName,
                      user.Discriminator,
                      user.FirstName,
                      user.LastName,
                      user.ProfilePhoto,
                      user.ParentCompanyId,
                      user.IsDeleted,
                      user.IsActive,
                      user.CreatedBy,
                      user.CreatedDate,
                      user.ModifiedBy,
                      user.ModifiedDate,
                      IsSystemAdmin = newRole != null
                      })
            .AsEnumerable().Select(x => new Core_User
            {
                Id = x.Id,
                Email = x.Email,
                EmailConfirmed = x.EmailConfirmed,
                PasswordHash = x.PasswordHash,
                SecurityStamp = x.SecurityStamp,
                PhoneNumber = x.PhoneNumber,
                PhoneNumberConfirmed = x.PhoneNumberConfirmed,
                TwoFactorEnabled = x.TwoFactorEnabled,
                LockoutEndDateUtc = x.LockoutEndDateUtc,
                LockoutEnabled = x.LockoutEnabled,
                AccessFailedCount = x.AccessFailedCount,
                UserName = x.UserName,
                Discriminator = x.Discriminator,
                FirstName = x.FirstName,
                LastName = x.LastName,
                ProfilePhoto = x.ProfilePhoto,
                ParentCompanyId = x.ParentCompanyId,
                IsDeleted = x.IsDeleted,
                IsActive = x.IsActive,
                CreatedBy = x.CreatedBy,
                CreatedDate = x.CreatedDate,
                ModifiedBy = x.ModifiedBy,
                ModifiedDate = x.ModifiedDate,
                IsSystemAdmin = x.IsSystemAdmin
            }).AsQueryable();

   return result;
}

0 个答案:

没有答案