【问题标题】:Update an Object by Matching with other Object Values depend on a Condition in C#通过与其他对象值匹配来更新对象取决于 C# 中的条件
【发布时间】:2020-04-24 15:05:54
【问题描述】:

我有如下所示的课程

public class Channels
    {
        internal string CallerExt { get; set; }
        internal string ChannelName { get; set; }
        internal string CalleeExt { get; set; }

        public Channels(string CallerExt, string ChannelName, string CalleeExt)
        {
            this.CallerExt = CallerExt;
            this.ChannelName = ChannelName;
            this.CalleeExt = CalleeExt;
        }
    }

如果所有值都是新的,我想添加 Channel 对象的列表,如果 CallerExtCaleeExt 已经存在于同一个对象中,那么 ChannelName 应该使用同一个对象的新值进行更新?

我设法找到包含 CallerExtCalleeExt 值与新值匹配的对象,如下所示:

public static List<Channels> activeChannels = new List<Channels>();

if (!string.IsNullOrWhiteSpace(caller) && !string.IsNullOrWhiteSpace(now) && !string.IsNullOrWhiteSpace(callee))
{
    var recentChannel = activeChannels.FirstOrDefault(c => c.CallerExt == caller && c.CalleeExt == callee);
}

但我不确定如何使用新的频道名称更新该特定对象?提前感谢您的帮助..

【问题讨论】:

  • 不清楚你有什么问题......我不认为“如何设置对象的属性”是你要问的,即使问题是这样读的。
  • 我的意思是一个对象的值
  • recentChannel.ChannelName = "new channel name"; 你问的是这个吗
  • 如果 CallerExt 和 CalleeExt 值与现有对象匹配,则频道名称应更新该特定对象的现有频道名称

标签: c# list linq object properties


【解决方案1】:

感谢那些试图回答我的问题的人,解决方案对我有用

if (!string.IsNullOrWhiteSpace(caller) && !string.IsNullOrWhiteSpace(now) && !string.IsNullOrWhiteSpace(callee))
{
    var recentChannel = activeChannels.FirstOrDefault(c => c.CallerExt == caller && c.CalleeExt == callee);
    if (recentChannel != null)
        recentChannel.ChannelName = now;
}                                      

【讨论】:

    【解决方案2】:

    你可以试试这种方式,现场演示here

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    public class Program
    {
        public static void Main()
        {
            var listOfChanels = new List<Channels>{ new Channels("1", "1", "1")};
    
            UpdateOrAdd(listOfChanels, new Channels("1", "2", "1"));
            UpdateOrAdd(listOfChanels, new Channels("2", "2", "2"));
            UpdateOrAdd(listOfChanels, new Channels("2", "3", "3"));
    
            foreach(var item in listOfChanels)
                Console.WriteLine(item.CalleeExt + item.CallerExt + item.ChannelName);
        }
    
        private static void UpdateOrAdd(List<Channels> list, Channels newObject)
        {
            var item = list.FirstOrDefault(p => p.CalleeExt == newObject.CalleeExt && p.CallerExt == newObject.CallerExt);
    
            if(item == null){
                list.Add(newObject);
            }else{
                item.ChannelName = newObject.ChannelName;
            }
        }
    
        public class Channels
        {
            internal string CallerExt { get; set; }
            internal string ChannelName { get; set; }
            internal string CalleeExt { get; set; }
    
            public Channels(string CallerExt, string ChannelName, string CalleeExt)
            {
                this.CallerExt = CallerExt;
                this.ChannelName = ChannelName;
                this.CalleeExt = CalleeExt;
            }
        }
    }
    

    【讨论】:

    • 这能回答你的问题吗?如果您需要任何帮助,请告诉我@Mohamed Farook Mohamed Fazrin
    猜你喜欢
    • 2020-09-11
    • 1970-01-01
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多