C# Getkey和value by,仅在值上除外
我从Dictionary()开始,并希望从中删除其值存在于另一个列表中的条目。下面的代码只返回一个列表。删除匹配值后,是否可以返回剩余键/值对的字典?如果没有,如何做到这一点C# Getkey和value by,仅在值上除外,c#,linq,C#,Linq,我从Dictionary()开始,并希望从中删除其值存在于另一个列表中的条目。下面的代码只返回一个列表。删除匹配值后,是否可以返回剩余键/值对的字典?如果没有,如何做到这一点 //pardon my pseudocode var pNames = new Dictionary<string, string>() { {key : "a1", value: "asdf_a1"}, {key : "a2", value: "asdf_a2"}, {key : "a
//pardon my pseudocode
var pNames = new Dictionary<string, string>()
{
{key : "a1", value: "asdf_a1"},
{key : "a2", value: "asdf_a2"},
{key : "a3", value: "asdf_a3"}
};
var bUsers = new List<string>() { "asdf_a2" };
var nUsers = pNames.Values.Except(bUsers);
//nUsers should be a Dictionary<string,string> containing two key/value pairs:
//{key : "a1", value: "asdf_a1"},
//{key : "a3", value: "asdf_a3"}
//请原谅我的伪代码
var pNames=新字典()
{
{键:“a1”,值:“asdf_a1”},
{键:“a2”,值:“asdf_a2”},
{键:“a3”,值:“asdf_a3”}
};
var bUsers=newlist(){“asdf_a2”};
var nUsers=pNames.Values.Exception(bUsers);
//nUsers应该是一个包含两个键/值对的字典:
//{键:“a1”,值:“asdf_a1”},
//{键:“a3”,值:“asdf_a3”}
由于您需要一个新词典,而不是从现有词典中删除条目,您可以使用Where
过滤组成字典的KeyValuePair
对象,然后使用ToDictionary
扩展方法将生成的IEnumerable
转换回字典
var nUsers=pNames
.Where(pn=>!bUsers.Contains(pn.Value))//这将是IEnumerable
.ToDictionary(x=>x.Key,x=>x.Value);
如果需要不区分大小写的匹配,请添加一个IEqualityComparer
作为Contains
方法的第二个参数。已尝试并测试
Dictionary<string, string> pNames = new Dictionary<string, string>(){
{"a1", "asdf_a1"},
{"a2", "asdf_a2"},
{"a3", "asdf_a3"}
};
var bUsers = new List<string>() { "asdf_a2" };
bUsers.ForEach(user =>
{
var Names = pNames.Where(name => name.Value == user).ToList();
foreach (KeyValuePair<string, string> Name in Names)
pNames.Remove(Name.Key);
});
Dictionary pNames=new Dictionary(){
{“a1”,“asdf_a1”},
{“a2”,“asdf_a2”},
{“a3”,“asdf_a3”}
};
var bUsers=newlist(){“asdf_a2”};
bUsers.ForEach(用户=>
{
var name=pNames.Where(name=>name.Value==user.ToList();
foreach(名称中的KeyValuePair名称)
pNames.Remove(Name.Key);
});
不完美。但你可以做类似的事情。这种方法的好处是可以与任何类型一起使用
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
private static void Main(string[] args)
{
Dictionary<string, string> pNames = new Dictionary<string, string>();
pNames.Add("a1", "value1");
pNames.Add("a2", "value2");
var bUsers = new List<string>() { "value1" };
var newKey = bUsers.ToDictionary(idx =>
{
return Guid.NewGuid().ToString();
}, x => x);
var result = pNames.Except(newKey, new CustomIEqualityComparer<string, string>());
}
public class CustomIEqualityComparer<TSource, TValue> : IEqualityComparer<KeyValuePair<TSource, TValue>>
{
public bool Equals(KeyValuePair<TSource, TValue> x, KeyValuePair<TSource, TValue> y)
{
return x.Value.Equals(y.Value);
}
public int GetHashCode(KeyValuePair<TSource, TValue> obj)
{
unchecked
{
int hash = 17;
hash = hash * 23 + obj.Value.GetHashCode();
return hash;
}
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
班级计划
{
私有静态void Main(字符串[]args)
{
Dictionary pNames=新字典();
pNames.添加(“a1”、“价值1”);
pNames.添加(“a2”、“价值2”);
var bUsers=newlist(){“value1”};
var newKey=bUsers.ToDictionary(idx=>
{
返回Guid.NewGuid().ToString();
},x=>x);
var result=pNames.Except(newKey,newcustomiequalitycomparer());
}
公共类CustomIEqualityComparer:IEqualityComparer
{
公共布尔等于(键值对x、键值对y)
{
返回x.Value等于(y.Value);
}
public int GetHashCode(KeyValuePair obj)
{
未经检查
{
int hash=17;
hash=hash*23+obj.Value.GetHashCode();
返回散列;
}
}
}
}
如果要与
类型一起使用,则代码相同:
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int index = 0;
private static void Main(string[] args)
{
Dictionary<int, string> pNames = new Dictionary<int, string>();
pNames.Add(1, "value1");
pNames.Add(2, "value2");
var bUsers = new List<string>() { "value1" };
var newKey = bUsers.ToDictionary(idx =>
{
return index++;
}, x => x);
var result = pNames.Except(newKey, new CustomIEqualityComparer<int, string>());
}
public class CustomIEqualityComparer<TSource, TValue> : IEqualityComparer<KeyValuePair<TSource, TValue>>
{
public bool Equals(KeyValuePair<TSource, TValue> x, KeyValuePair<TSource, TValue> y)
{
return x.Value.Equals(y.Value);
}
public int GetHashCode(KeyValuePair<TSource, TValue> obj)
{
unchecked
{
int hash = 17;
hash = hash * 23 + obj.Value.GetHashCode();
return hash;
}
}
}
}
使用系统;
使用系统集合;
使用System.Collections.Generic;
使用System.Linq;
班级计划
{
静态int指数=0;
私有静态void Main(字符串[]args)
{
Dictionary pNames=新字典();
pNames.添加(1,“价值1”);
pNames.添加(2,“价值2”);
var bUsers=newlist(){“value1”};
var newKey=bUsers.ToDictionary(idx=>
{
返回索引++;
},x=>x);
var result=pNames.Except(newKey,newcustomiequalitycomparer());
}
公共类CustomIEqualityComparer:IEqualityComparer
{
公共布尔等于(键值对x、键值对y)
{
返回x.Value等于(y.Value);
}
public int GetHashCode(KeyValuePair obj)
{
未经检查
{
int hash=17;
hash=hash*23+obj.Value.GetHashCode();
返回散列;
}
}
}
}
您可以进一步细化和改进
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
class Program
{
static int index = 0;
private static void Main(string[] args)
{
Dictionary<int, string> pNames = new Dictionary<int, string>();
pNames.Add(1, "value1");
pNames.Add(2, "value2");
var bUsers = new List<string>() { "value1" };
var newKey = bUsers.ToDictionary(idx =>
{
return index++;
}, x => x);
var result = pNames.Except(newKey, new CustomIEqualityComparer<int, string>());
}
public class CustomIEqualityComparer<TSource, TValue> : IEqualityComparer<KeyValuePair<TSource, TValue>>
{
public bool Equals(KeyValuePair<TSource, TValue> x, KeyValuePair<TSource, TValue> y)
{
return x.Value.Equals(y.Value);
}
public int GetHashCode(KeyValuePair<TSource, TValue> obj)
{
unchecked
{
int hash = 17;
hash = hash * 23 + obj.Value.GetHashCode();
return hash;
}
}
}
}