Bool值无法更改

时间:2016-03-24 07:36:02

标签: c# boolean

我想在有效期结束后自动停用用户。因此,在查看用户帐户详细信息时,超过到期日期的用户帐户应显示为非活动状态(使用fa-toggle打开/关闭 - 显示帐户的活动/非活动状态 - 在代码中它是bool值true / false )。

现有

  1. 点击获取帐户详细信息时,
  2. 将从显示的数据库中提取详细信息,
  3. 提取后,它将显示在应用程序中
  4. 我做的是什么?

    1. 点击获取帐户详细信息时,
    2. 将从显示的数据库中提取详细信息,
    3. 在向最终用户显示之前
    4. 我正在尝试通过将当前日期与到期日期进行比较来更改客户的有效/无效细节。
    5. 提取后,它将显示在应用程序中
    6. 现有代码:

      public IEnumerable<CustomerDetails> GetCustomerDetails(string groupName)
      {
          IEnumerable<CustomerDetails> customers;
      
          var query = "select up.username, up.firstname, up.lastname, up.lastactivitydate, up.expirydate, " +
                                  "up.activationdate, up.licensedby, up.position, up.isactive as isActiveUser, " +
                                  "wr.rolename, up.usergroup, ul.contracttype as contracttype, ul.numberoflicense as " +
                                  "licensesallowed from userprofile up inner join userlicense ul on ul.id = up.firmid inner join " +
                                  "webpages_UsersInRoles wur on wur.userid =  up.userid inner join webpages_Roles wr on " +
                                  "wr.roleid = wur.roleid where ul.firmname='" + firmname.Replace("'", "''") + "' order by up.lastname";
      
          using (IDbConnection dbConnection = new SqlConnection(UserDBConnectionString))
          {
              dbConnection.Open();
      
              using (var result = dbConnection.QueryMultiple(query, commandTimeout: 300))
              {
                  var accounts = result.Read().ToList();
      
                  customers = from account in accounts
                                               select new CustomerDetails
                                               {
                                                   FirstName = account.firstname,
                                                   LastName = account.lastname,
                                                   UserName = account.username,
                                                   PreviousUserName = account.username,
                                                   Position = account.position,
      
                                                   SharedSpace = JsonConvert.DeserializeObject<string[]>(account.usergroup),
      
                                                   UserRole = account.rolename,
                                                   IsActiveUser = account.isActiveUser,
      
                                                   LastActivityDate = account.lastactivitydate != null
                                                   ? account.lastactivitydate.ToString("yyyy/MM/dd")
                                                   : "",
      
                                                   ExpiryDate = TimeZoneInfo.ConvertTimeToUtc((DateTime)account.expirydate),
      
                                                   ActivationDate = TimeZoneInfo.ConvertTimeToUtc((DateTime)account.activationdate),
      
                                                   ContractType = account.contracttype.ToString().Trim(),
      
                                                   LicensesAllowed = account.licensesallowed
                                               };
              }
          }
          return customers;
      }
      

      我做的是在回复客户详细信息之前return customers;。我已经包含以下一组代码来停用所有超过激活期的用户。但它没有按预期工作。

      // code as above shown
      var count = customers.Count();
      for (var i = 0; i < count;i++)
      {
          DateTime dateLimit = Convert.ToDateTime(customers.ElementAt(i).ExpiryDate); 
          DateTime currentDate = DateTime.Now.Date;
          int result = DateTime.Compare(dateLimit,currentDate);
          if (result == -1)
          {
              if (customers.ElementAt(i).IsActiveUser == true)
              {
                  var beforeChanged = customers.ElementAt(i).IsActiveUser; // before it is true
      
                  customers.ElementAt(i).IsActiveUser = false; // i hope i am setting the value as false
      
                  var afterChanged = customers.ElementAt(i).IsActiveUser; // but i am still seeing true only here               
      
              }                    
          }
      }
      return customers
      }
      

      编辑-1:

      enter image description here

      正如我在评论中所说,我可以将此处的值从true更改为false,反之亦然。

      编辑-2:

      //客户类结构

      public class CustomerDetails
      {
          public string FirstName { get; set; }
      
          public string LastName { get; set; }
      
          public bool IsActiveUser { get; set; }
          // some other fields ... goes here
      }
      

      在将值设置为false后,我不知道它是如何仍然存在的。

2 个答案:

答案 0 :(得分:7)

您是deferred execution的受害者。

每次拨打customers时,您都会在数据库中查询三次

var beforeChanged = customers.ElementAt(i).IsActiveUser; 

customers.ElementAt(i).IsActiveUser = false;

var afterChanged = customers.ElementAt(i).IsActiveUser;

每次从数据库加载对象,这就是您没有看到任何更改的原因。数据库没有变化。

您需要实现查询,可以在构建选择时致电.ToArray()ToList()客户。

答案 1 :(得分:2)

问题:客户是IEnumerable / IQueryable,而不是固定列表。它是如何传递对象&#34;的定义,而不是实际或稳定对象的列表。 每个对&#39;元素的引用&#39;导致customers查询其来源(列表&lt;&gt;),并在检索它们时创建全新的对象。每一个...单...时间。

解决方案:确保使用ToArrray()或ToList()等持久性函数创建customers