说我有以下列表:
List 1:
{{"John", "Doe", "Tall", "Old"},
{"John", "Doe", "Short", "Old"},
{"Jane", "Doe", "Tall", "Young"},
{"Jane", "Doe", "Short", "Old"}}
我想在列表中搜索{“John”,“Doe”,“Short”,“Old”}。
搜索此嵌套列表条目的最佳方法是什么,并确保我不会获得{“John”,“Doe”,“Tall”,“Old”}?
如果嵌套列表只包含一个string
项而不是四个,我会使用LINQ来展平列表并搜索结果List<string>.
,即:
List<string> newList = oldList.SelectMany(x => x).Distinct().ToList();
newList.Contains("string");
对于每个嵌套列表包含多个字符串项的列表,我能做些类似的事情吗?
答案 0 :(得分:6)
那么列表必须包含该顺序中的所有字符串?然后你可以使用Enumerable.SequenceEqual
。如果订单不重要,请将Enumerable.All
与Contains
一起使用,以便:
var names = new[]{"John", "Doe", "Short", "Old"};
List<List<string>> result = list
.Where(l => l.SequenceEqual(names)).ToList();
或
result = list
.Where(l => l.All(name => names.Contains(name))).ToList();
输出:
foreach(List<string> l in result)
Console.WriteLine(string.Join(",", l)); // John,Doe,Short,Old
旁注:如果您将搜索的集合转换为HashSet<T>
,则可以提高第二种方法的效率:
var names = new HashSet<string>(new[]{"John", "Doe", "Short", "Old"});
result = list.Where(l => l.All(names.Contains)).ToList();
正如Servy所提到的,第二种方法并不能阻止你获得包含所有项目的列表,而且还有更多。您可以添加Count
检查以确保它。
答案 1 :(得分:2)
您可以使用允许传递自定义相等比较器(IEqualityComparer<T>
)的Contains
方法的重载版本。
答案 2 :(得分:2)
“扁平化”清单的一种方法如下:
var people = list1.
Select(lst => new {
First = lst[0]
, Last = lst[1]
, Height = lst[2]
, Age = lst[3]
});
现在您可以按如下方式搜索收容:
bool hasShortOldJohnDoe = people
.Contains(p => p.First=="John"
&& p.Last=="Doe"
&& p.Height == "Short"
&& p.Age=="Old");
答案 3 :(得分:1)
您可以尝试以下操作:
List<List<string>> mainList = new List<List<string>>
{
new List<string>(){"John", "Doe", "Tall", "Old"},
new List<string>(){"John", "Doe", "Short", "Old"},
new List<string>(){"Jane", "Doe", "Tall", "Young"},
new List<string>(){"Jane", "Doe", "Short", "Old"},
};
List<string> searchList = new List<string>() { "John", "Doe", "Short", "Old" };
var temp = mainList[0].Except(searchList).Count();
List<List<string>> result = mainList
.Where(r => r.Except(searchList).Count() == 0)
.ToList();
它会在result
中为您提供一个项目。
或者:
var result = mainList
.Where(r => !r.Except(searchList).Any());
答案 4 :(得分:1)
以下是两种方式:
[Test]
public void Using_String_Join()
{
var l = new List<List<string>>
{
new List<string> {"John", "Doe", "Tall", "Old"},
new List<string> {"John", "Doe", "Short", "Old"},
new List<string> {"Jane", "Doe", "Tall", "Young"},
new List<string> {"Jane", "Doe", "Short", "Old"}
};
var l2 = new List<string> {"John", "Doe", "Short", "Old"};
Assert.That(l.Count(inner => string.Join(",", inner).Equals(string.Join(",", l2))), Is.EqualTo(1));
}
[Test]
public void Using_SequenceEqual()
{
var l = new List<List<string>>
{
new List<string> {"John", "Doe", "Tall", "Old"},
new List<string> {"John", "Doe", "Short", "Old"},
new List<string> {"Jane", "Doe", "Tall", "Young"},
new List<string> {"Jane", "Doe", "Short", "Old"}
};
var l2 = new List<string> {"John", "Doe", "Short", "Old"};
Assert.That(l.Count(inner => inner.SequenceEqual(l2)), Is.EqualTo(1));
}