[英]How to filter a list of tuples based on matching one item of the tuple to another list through lambda expression in C#?
我有一个元组列表:
List<Tuple<string, string>> keys = { ("AB","12"), ("BC","23"), ("XY","00")}
我有另一个可枚举的字符串集合:
IEnumerable<string> results = {"ABC", "BCD", "ZZXY"}
我正在尝试编写一个lambda表达式,它将为我提供所有键的列表,以便有一个以keys.item1
开头的相应结果(在results
列表中)。
最后,我想要以下内容:
List<Tuple<string, string>> finalKeys = { ("AB","12"), ("BC","23") }
使用带有相应Any
调用的Where
语句:
List<Tuple<string, string>> finalKeys = keys
.Where(key => results.Any(result => result.StartsWith(key.Item1)))
.ToList();
尝试:
List<Tuple<string, string>> keys = new List<Tuple<string, string>>
{
new Tuple<string, string>("AB", "12"),
new Tuple<string, string>("BC", "23"),
new Tuple<string, string>("XY", "00")
};
IEnumerable<string> results = new List<string> {"ABC", "BCD", "ZZXY"};
var finalKeys = keys.Where(f => results.Any(m => m.StartsWith(f.Item1))).ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.