[英]Search in List of List in C#
I have a list ( List<a>
) that contains a list ( List<b>
). 我有一个列表( List<a>
),其中包含一个列表( List<b>
)。 There is a string field in b type list. b类型列表中有一个字符串字段。 I want to to find the indexes of matching strings in list b by searching list a. 我想通过搜索列表a在列表b中找到匹配字符串的索引。 How can i do that? 我怎样才能做到这一点?
public class b
{
string Word;
bool Flag;
}
public class a
{
List<b> BList = new List<b>();
int Counter;
}
I want to find indexes in list b that matches with the string "Word". 我想在列表b中找到与字符串“ Word”匹配的索引。
This Linq Expression return a List of BList and proper finded Index: 此Linq表达式返回BList列表和正确找到的索引:
var Result = AList.Select(p => new
{
BList = p.BList,
indexes = p.BList.Select((q, i) => new
{
index = i,
isMatch = q.Word == "Word"
}
)
.Where(q => q.isMatch)
.Select(q=>q.index)
});
Is that what you need? 那是你需要的吗?
var alist = GetListA();
var indexes = alist.Select((ix, a) =>
a.BList.SelectMany((jx, b) =>
new {AIx = ix, BIx = jx, b.Word}))
.Where(x => x.Word == pattern)
.Select(x => new {x.AIx, x.BIx});
I guess this depends on what you want as an output - this will give you a projection like: 我想这取决于您想要的输出-这将为您提供一个像这样的投影:
indexes[0] { A = A[0], Indexes = {1,5,6,7} }
indexes[1] { A = A[1], Indexes = {4,5,8} }
...
var indexes = listA
.Select(a => new
{
A = a,
Indexes = a.BList
.Select((b, idx) => b == wordToCheck ? idx : -1)
.Where(i => i > -1)
});
这使您所有满足女巫“单词”的对象:
from a in aList from b in a.bList where b.word.Equals("word") select b;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.