How to check if an element inside a List of string array exists in another List of string array

huangapple go评论53阅读模式
英文:

How to check if an element inside a List of string array exists in another List of string array

问题

I basically want to know if a string array from one list exists in another list of string array

So for example:
List 1:
{"A", "R1"}
{"A", "R2"}

List 2:
{"A", "R1"}
{"B", "R1"}
{"B", "R2"}

Then If I loop in List 1 to check whether it exists in List 2 the results will be:
true
false

I want to do it in LINQ if possible.

英文:

I basically want to know if a string array from one list exists in another list of string array

So for example:<BR>
List 1:<BR>
{"A", "R1"}<BR>
{"A", "R2"}<BR>

List 2:<BR>
{"A", "R1"}<BR>
{"B", "R1"}<BR>
{"B", "R2"}<BR>

Then If I loop in List 1 to check whether it exists in List 2 the results will be:<BR>
true<BR>
false

I want to do it in LINQ if possilbe

答案1

得分: 3

你可以使用 Any  SequenceEqual 进行这个操作。

using System.Collections.Generic;

List<string[]> testSet = new List<string[]>
{
    new string[] { "A", "R1" },
    new string[] { "A", "R2" }
};

List<string[]> resultSet = new List<string[]>
{
    new string[] { "A", "R1" },
    new string[] { "B", "R1" },
    new string[] { "B", "R2" }
};

// 检查值是否在列表中的任何位置找到
bool IsArrayInList(string[] value, List<string[]> list)
{
    return list.Any(value.SequenceEqual);
}

foreach (var val in testSet)
{
    Console.WriteLine(IsArrayInList(val, resultSet));
}

Console.ReadLine();
英文:

You can use

> Any and SequenceEqual

for this.

using System.Collections.Generic;

List&lt;string[]&gt; testSet = new List&lt;string[]&gt;
{
    new string[] { &quot;A&quot;, &quot;R1&quot; },
    new string[] { &quot;A&quot;, &quot;R2&quot; }
};

List&lt;string[]&gt; resultSet = new List&lt;string[]&gt;
{
    new string[] { &quot;A&quot;, &quot;R1&quot; },
    new string[] { &quot;B&quot;, &quot;R1&quot; },
    new string[] { &quot;B&quot;, &quot;R2&quot; }
};

// Checks if the value is found anywhere in the list
bool IsArrayInList(string[] value, List&lt;string[]&gt; list)
{
    return list.Any(value.SequenceEqual);
}

foreach (var val in testSet)
{
    Console.WriteLine(IsArrayInList(val,resultSet));
}

Console.ReadLine();

答案2

得分: 2

我建议结合.Any().SequenceEqual().All()来使用。

示例:

var list1 = new string[][]
{
    new string[] { "A", "R1" },
    new string[] { "A", "R2" },
};
var list2 = new string[][]
{
    new string[] { "A", "R1"},
    new string[] { "B", "R1"},
    new string[] { "B", "R2"},
};
var itemToFind = list1.First();
// 选项1
var isInList2 = list2.Any(i => i.SequenceEqual(itemToFind));
// 选项2
var isInList2Option2 = list2.Any(item => item.All(innerItem => itemToFind.Contains(innerItem)));

根据需要,您可以改进这些方法,以考虑顺序、不区分大小写、确切参数数量等等...
选项1将找到任何匹配项,其中内部序列按与您查找的相同的顺序、相同大小写的方式排列。
选项2将找到任何匹配项,其中内部序列包含您要检查的序列的所有值。

希望这至少能指导您朝正确的方向前进。

英文:

I would suggest a combination of .Any() and .SequenceEqual() or .All()

Example:

var list1 = new string[][]
{
    new string[] { &quot;A&quot;, &quot;R1&quot; },
    new string[] { &quot;A&quot;, &quot;R2&quot; },
};
var list2 = new string[][]
{
    new string[] { &quot;A&quot;, &quot;R1&quot;},
    new string[] { &quot;B&quot;, &quot;R1&quot;},
    new string[] { &quot;B&quot;, &quot;R2&quot;},
};
var itemToFind = list1.First();
// Option 1
var isInList2 = list2.Any(i =&gt; i.SequenceEqual(itemToFind));
// Option 2
var isInList2Option2 = list2.Any(item =&gt; item.All(innerItem =&gt; itemToFind.Contains(innerItem)));

As-needed, you can improve these to account for ordering, case-insensitivity, exact number of arguments, etc...
Option 1 will find any matches where the inner sequence is in the same order, same-casing as what you are looking for.
Option 2 will find any matches where the inner sequence contains all of the values of the sequence you are checking for.

Hopefully this points you in the right direction at least.

huangapple
  • 本文由 发表于 2023年2月17日 23:40:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/75486373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定