コード例 #1
0
        /// <summary>
        /// 根据指定的查询参数查询用户句柄。
        /// </summary>
        /// <param name="query">查询参数。</param>
        /// <returns>一个包含了所有满足查询条件的用户句柄的查询结果对象。</returns>
        /// <exception cref="ArgumentNullException"/>
        public ReadOnlyCollection <UserHandle> QueryUsers(UserQueryParameter query)
        {
            if (query == null)
            {
                throw new ArgumentNullException(nameof(query));
            }
            if (query.QueryByOrganization && query.Organization == null)
            {
                throw new ArgumentNullException(nameof(query.Organization));
            }

            bool hasQuery = query.QueryByOrganization || query.QueryByUsergroup;

            if (!hasQuery)
            {
                // 没有查询参数。返回空列表。
                return(new ReadOnlyCollection <UserHandle>(new List <UserHandle>()));
            }
            else
            {
                return(m_factory.WithContext(context =>
                {
                    IQueryable <UserProfileEntity> profiles = context.GetAllUserProfiles();
                    if (query.QueryByOrganization)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesByOrganization(profiles, query.Organization);
                    }
                    if (query.QueryBySex)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesBySex(profiles, (NativeUserSex)query.Sex);
                    }
                    if (query.QueryByUsergroup)
                    {
                        profiles = UserDataContext.QueryUserProfileEntitiesByUsergroup(profiles,
                                                                                       (NativeUserGroup)query.UserGroup);
                    }

                    // 对查询结果进行排序以准备随时分页。
                    profiles.OrderBy(item => item.Username);

                    if (query.EnablePageQuery)
                    {
                        profiles = profiles.Page(query.PageQuery);
                    }

                    List <UserHandle> handles = new List <UserHandle>();
                    foreach (UserProfileEntity ent in profiles)
                    {
                        handles.Add(UserHandle.FromUserProfileEntity(ent));
                    }

                    return new ReadOnlyCollection <UserHandle>(handles);
                }));
            }
        }