-1

これは、データベースから非アクティブ/アクティブ ユーザーのリストを取得するためのクエリです。私の問題は、クエリが複雑すぎるように見えることです。私のクエリを強化する方法を教えてください。

これが私のコードです

            using (basecampcoreEntities dbs = ConfigAndResource.BaseCampContext())
        {
            //loads all user where isactive property has the same value as IsActive
            var Users = from useritem in dbs.users
                        where useritem.useraccount.IsActive.Equals(IsActive)
                        orderby useritem.useraccount.CreatedDate
                        select useritem;

            //check if users count is greater than 0
            if (Users.Count() > 0)
            {

                List<user> CasebookUser = new List<user>();
                switch (SearchBy)
                {
                    case DTO::SearchBy.FirstName:
                        {
                            CasebookUser = Users.Where(item => item.FirstName.ToUpper().Equals(SearchText.ToUpper())).Skip(skip).Take(take).ToList();
                        } break;
                    case DTO::SearchBy.LastName:
                        {
                            CasebookUser = Users.Where(item => item.LastName.ToUpper().Equals(SearchText.ToUpper())).Skip(skip).Take(take).ToList();
                        } break;
                    case DTO::SearchBy.LoginID:
                        {
                            CasebookUser = Users.Where(item => item.LoginID.ToUpper().Equals(SearchText.ToUpper())).Skip(skip).Take(take).ToList();
                        } break;
                    case DTO::SearchBy.None:
                            CasebookUser = Users.Skip(skip).Take(take).ToList();
                        {
                        } break;
                }

                //transform the data into DTO class
                return (from item in CasebookUser
                        select new DTO::User
                        {
                            LoginID = item.LoginID,
                            FirstName = item.FirstName,
                            LastName = item.LastName,
                            MiddleName = item.MiddleName,
                            Birhtday = item.userinfo != null ? item.userinfo.Birthday : DateTime.UtcNow
                        }).ToList();
            }
            return null;
4

2 に答える 2

2

ここで遅延実行の力を活用してください...

// first filter by whether user is active or not
var query = dbs.users.Where(x => x.useraccount.IsActive == IsActive);    

// next filter by specific search field
switch (SearchBy)
{
    case DTO::SearchBy.FirstName:
    {
        query = query.Where(x => string.Equals(x.FirstName, SearchText, StringComparison.InvariantCultureIgnoreCase)); 
        break;
    }
    case DTO::SearchBy.LastName:
    {
        query = Users.Where(x => string.Equals(x.LastName, SearchText, StringComparison.InvariantCultureIgnoreCase));
        break;
    }
    ...
}

// then apply paging
query = query.Skip(skip).Take(take);

// finally, order by CreatedDate (ascending)
query = query.OrderBy(x => x.useraccount.CreatedDate);

// now fetch the records!
return (from item in query
        select new DTO::User
        {
            LoginID = item.LoginID,
            FirstName = item.FirstName,
            LastName = item.LastName,
            MiddleName = item.MiddleName,
            Birhtday = item.userinfo != null ? item.userinfo.Birthday : DateTime.UtcNow
         }).ToList();

このコードは、より最適な方法で必要なものを提供します (1 回の DB トリップのみ)。

于 2013-09-30T08:00:40.950 に答える