簡體   English   中英

如何通過C#中的lambda表達式將元組的一個項匹配到另一個列表來過濾元組列表?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM