添加和更新List对象在C#中不起作用

时间:2017-07-04 08:58:45

标签: c# linq

新手用C#和Linq。

我正在尝试添加一个对象并更新一个对象(如果存在于List中)。该列表没有得到更新或没有添加新的输入。它不会抛出任何异常。请帮助。

private IList<SettingsEntity> Settings = new List<SettingsEntity>();

public SettingsRepository() {
            Settings.Add(new SettingsEntity() { UserID = "alpha", ApplicationName = "protocol archive", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "bravo", ApplicationName = "view", SettingKey = "RefreshRate", SettingValue = "10", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "charlie", ApplicationName = "upload", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "foxtrot", ApplicationName = "protocol archive", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "lima", ApplicationName = "view", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "Julie", ApplicationName = "upload", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "tango", ApplicationName = "protocol archive", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "victor", ApplicationName = "protocol archive", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "yankee", ApplicationName = "protocol archive", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
            Settings.Add(new SettingsEntity() { UserID = "zulu", ApplicationName = "protocol archive", SettingKey = "RefreshTime", SettingValue = "20", UpdatedOn = new DateTime().Date, SettingLabel = "RefreshInterval", IsActive = true });
        }

 public bool Save(SettingsEntity entity)
        {
            var flag = false;
            if (entity != null)
            {
                var existing = Settings.Where(se => se.UserID == entity.UserID && se.ApplicationName == entity.ApplicationName && se.SettingKey == entity.SettingKey).ToList();
                if (existing != null)
                {
                    existing.ForEach(f => f.SettingValue = entity.SettingValue);
                    flag = true;
                }
                else
                {
                    Settings.Add(entity);
                    flag = true;
                }
                return flag;
            }
            else {
                throw new ArgumentNullException();
            }


        }

 [HttpPost]
        public IActionResult Post([FromBody] SettingsEntity entity)
        {
            var result = _SettingsRepository.Save(entity);
            if (result == true)
            {
                return Ok();
            }
            else
            {
                return BadRequest();
            }
        }

3 个答案:

答案 0 :(得分:1)

var existing = Settings.Where(se => se.UserID == entity.UserID 
                                 && se.ApplicationName == entity.ApplicationName 
                                 && se.SettingKey == entity.SettingKey).ToList();

永远不会是null

因此永远不会调用Settings.Add(entity)。而不是测试null你可以写

if (existing.Any()) //or if (existing.Count > 0)
{ 
    ... 
} 
else 
{ 
    //Save here 
}

答案 1 :(得分:0)

Linq查询始终返回具有零个或多个零元素的Ienumerable集合。所以,它永远不会是空的。此外,您也可以跳过标志变量。通过计数检查来确保添加了某些内容。

或者尽量避免使用Count方法。因为它具有o(n)的复杂性。所以,如果你只是想确保是否有任何项目,那么更喜欢使用Any()方法而不是Count();

答案 2 :(得分:0)

这不适合你吗?你试图得到的List永远不会为空(怎么可能?你从有效的IEnumerable得到它)所以你能做的就是检查List中的元素数量。

顺便说一下。您将始终返回true,因此我将其删除以使您的代码更具可读性。

public void Save(SettingsEntity entity)
{
    if (entity == null)
        throw new ArgumentNullException();

    var existing = Settings.Where(se => se.UserID == entity.UserID && se.ApplicationName == entity.ApplicationName && se.SettingKey == entity.SettingKey).ToList();
    if (existing.Count > 0)
        existing.ForEach(f => f.SettingValue = entity.SettingValue);
    else
        Settings.Add(entity);
}