Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何添加更新删除重复的键值对列表_C#_Linq - Fatal编程技术网

C# 如何添加更新删除重复的键值对列表

C# 如何添加更新删除重复的键值对列表,c#,linq,C#,Linq,我需要有一个通用方法,以便从重复的键值对列表中添加Update Delete 我的重复键值对如下所示 List<KeyValuePair<string, int>> dupesStudentIdsList = new List<KeyValuePair<string, int>>(); dupesStudentIdsList.Add(new KeyValuePair<string, int>("1234_

我需要有一个通用方法,以便从重复的键值对列表中添加Update Delete

我的重复键值对如下所示

        List<KeyValuePair<string, int>> dupesStudentIdsList = new List<KeyValuePair<string, int>>();
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("1234_456X", 1));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("1234_456X", 2));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("1234_456X", 3));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("1234_456X", 4));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("1234_456X", 5));
        //new set of duplicate ids with increasing rowNumber 
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("9999_999A", 1));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("9999_999A", 2));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("9999_999A", 4));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("9999_999A", 5));
        dupesStudentIdsList.Add(new KeyValuePair<string, int>("9999_999A", 6));
//密钥作为“1234_456X”传递时的预期结果

现在我只想通过传递键和值来删除一条记录(“9999999a”,6)

//预期结果

("9999_999A", 1));
("9999_999A", 2));
("9999_999A", 4));
("9999_999A", 5));  // last row is removed. 
("9999_999A", 1));
("9999_999A", 2));
("9999_999A", 4));
("9999_999A", 999999));  //only value is updated for that key and value 
//仅更新传递键和值的值

  private void updateValueOnly(string key , int Value) // Only Value update for the Key
    {
    }
//预期结果

("9999_999A", 1));
("9999_999A", 2));
("9999_999A", 4));
("9999_999A", 5));  // last row is removed. 
("9999_999A", 1));
("9999_999A", 2));
("9999_999A", 4));
("9999_999A", 999999));  //only value is updated for that key and value 

这就是我所做的。如果有更好的方法,我将不胜感激

   private void RemoveDupesbyKey(string DupeKey) //passing "1234_456X"
{
    dupesStudentIdsList.RemoveAll(x => x.Key.Equals(Dupekey));
}

private void RemoveByPassingKeyandValue(string key, int Value) // this is passed
{
    dupesStudentIdsList.Remove(new KeyValuePair<string, int>(key, Value));
}
private void RemoveDupesbyKey(字符串DupeKey)//传递“1234_456X”
{
RemoveAll(x=>x.Key.Equals(Dupekey));
}
private void RemoveByPassingKeyandValue(字符串键,int值)//这是传递的
{
删除(新的KeyValuePair(key,Value));
}
因为KeyValuePair是不可变的。我们需要删除它并添加它

private void UpdateValueOnly(string key, int Value) // this is passed
{

  if (dupesStudentIdList.Where(x => x.Key.Equals(key) && x.Value.Equals(Value).Any()))//check if it matches 
    {
        //remove
        dupesStudentIdsList.Remove(new KeyValuePair<string, int>(key, Value));
        // add 
        dupesStudentIdsList.Add(new KeyValuePair<string, int>(key, Value));
    }

}
private void UpdateValueOnly(字符串键,int值)//这是传递的
{
if(dupesStudentIdList.Where(x=>x.Key.Equals(Key)&&x.Value.Equals(Value.Any())//检查是否匹配
{
//除去
删除(新的KeyValuePair(key,Value));
//加
添加(新的KeyValuePair(key,Value));
}
}
private void UpdateValueOnly(string key, int Value) // this is passed
{

  if (dupesStudentIdList.Where(x => x.Key.Equals(key) && x.Value.Equals(Value).Any()))//check if it matches 
    {
        //remove
        dupesStudentIdsList.Remove(new KeyValuePair<string, int>(key, Value));
        // add 
        dupesStudentIdsList.Add(new KeyValuePair<string, int>(key, Value));
    }

}