c#在字符串中使用获取可行值,然后在查找中搜索,然后按字符串限制搜索
具有如下查找结构的c#在字符串中使用获取可行值,然后在查找中搜索,然后按字符串限制搜索,c#,C#,具有如下查找结构的 public class LookupEntry { public string Key { get; set; } public string Value { get; set; } } //inside main var list = new List<LookupEntry>(new LookupEntry[] { new LookupEntry() {Key="A", Value="mo" }, new LookupE
public class LookupEntry
{
public string Key { get; set; }
public string Value { get; set; }
}
//inside main
var list = new List<LookupEntry>(new LookupEntry[]
{
new LookupEntry() {Key="A", Value="mo" },
new LookupEntry() {Key="A", Value="nu"},
new LookupEntry() {Key="B", Value="ag"},
new LookupEntry() {Key="B", Value="bi"},
new LookupEntry() {Key="B", Value="cu"},
new LookupEntry() {Key="C", Value="tu"},
new LookupEntry() {Key="D", Value="uo"},
new LookupEntry() {Key="D", Value="vu"},
new LookupEntry() {Key="D", Value="zu"},
new LookupEntry() {Key="E", Value="ve"}
});
string original = "AD";
string large = "mobivecuvumonubinuzumozu";
// ABEBDAABADAD would be codified string
用于广告字符串
但我不想从查找中获取所有组合,而是想限制使用大字符串的搜索,以获取可能的组合
如果是那样的话,我会
“努祖”
“墨祖”
我在做类似的事情
var lookup = list.ToLookup(x => x.Key, x => x.Value);
var allAs = lookup["A"].ToList();
var allDs = lookup["D"].ToList();
但我不知道如何继续,首先搜索所有可能性,然后删除不在字符串中的可能性是一个好的选择吗?您可以尝试以下想法:
var query = allAs.SelectMany(x => allDs, (x, y) => x + y).Where(large.Contains);
这对两个列表进行笛卡尔连接,并根据结果是否在大字符串中进行过滤
或者,在LINQ查询语法中,它如下所示:
var query = from x in allAs
from y in allDs
where large.Contains(x + y)
select x + y;
输出:
墨祖
努祖
.好的,我看到你编辑了,现在,你想限制字符串的内容?…结果的数量是说,如果有10个结果,你想用某个数字或预定义的数字限制?…因为在你的示例5中可能会有类似的结果,你需要限制到示例2,例如,哪两个结果最先出现2?。。。
var query = from x in allAs
from y in allDs
where large.Contains(x + y)
select x + y;