-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 回答
1

我会:

  1. 从原始查询定义中删除排序
  2. 不要过早地将ToList()and 转换为Skip()' andTake()`
  3. 不要施放ToList()两次。只有在创建最终集合时才这样做。
  4. 在初始查询中使IsActive比较更清晰
  5. 将第一个查询重写为一行 linq 表达式
  6. 使用Any()代替Count() > 0
  7. 一次转换您的搜索文本ToUpper()。使您的搜索案例更简洁易读(与使用 == 而不是相同Equals

此代码可能会有所帮助:

using (basecampcoreEntities dbs = ConfigAndResource.BaseCampContext())
{
  //loads all user where isactive property has the same value as IsActive
  var Users = db.Users.Where(x => x.useraccount.IsActive == IsActive);

  if (Users.Any())
  {
    var searchText = SearchText.ToUpper();
    switch (SearchBy)
    {
      case DTO::SearchBy.FirstName:
        Users = Users.Where(item => item.FirstName.ToUpper() == searchText);
        break;
      case DTO::SearchBy.LastName:
        Users = Users.Where(item => item.LastName.ToUpper() == searchText);
        break;
      case DTO::SearchBy.LoginID:
        Users = Users.Where(item => item.LoginID.ToUpper() == searchText);
        break;
   }

   // apply sort and skip/take
   Users = Users.OrderBy(x => x.useraccount.CreateDate).Skip(skip).Take(take);

   //transform the data into DTO class
   return (from item in Users 
           select new DTO::User
           {
              LoginID = item.LoginID,
              FirstName = item.FirstName,
              LastName = item.LastName,
              MiddleName = item.MiddleName,
              Birthday = item.userinfo != null ? item.userinfo.Birthday : DateTime.UtcNow
           }).ToList();
    }
    return null;
}
于 2013-09-30T08:05:20.260 回答