Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何搜索两个列表成员?_C#_String_Visual Studio 2010_List_Hashset - Fatal编程技术网

C# 如何搜索两个列表成员?

C# 如何搜索两个列表成员?,c#,string,visual-studio-2010,list,hashset,C#,String,Visual Studio 2010,List,Hashset,我有以下课程: public class fixFirstDuplicate { public string firstParam { get; set; } public string secondParam { get; set; } } 名单如下: public static List<fixFirstDuplicate> firstDuplicate = new List<fixFirstDuplicate>

我有以下课程:

    public class fixFirstDuplicate
    {
        public string firstParam { get; set; }
        public string secondParam { get; set; }
    }
名单如下:

public static List<fixFirstDuplicate> firstDuplicate = new List<fixFirstDuplicate>();
publicstaticlist firstDuplicate=newlist();
列表中充满了firstParam和secondParam的值,它们彼此对应。它基本上是一个表,其中第一个firstParam与第一个secondParam相关联,依此类推

那么我有:

List<string> conLines = new List<string>();
List conLines=新列表();
我想查看conLines,每当一个字符串同时包含firstParam和secondParam时,对该字符串执行一个方法


如果我只使用一个列表,比如firstParam,我会使用HashSet,但是如果我有firstParam和secondParam,我不知道如何做等效的操作您需要检查
conLines
是否同时包含
firstParam
secondParam

var query = firstDuplicate.Where(r => conLines.Contains(r.firstParam) &&
                                   conLines.Contains(r.secondParam));
编辑:如果要在找到匹配项时调用方法,并且顺序对您很重要,则可以使用简单的
foreach
循环,如:

foreach (var item in firstDuplicate)
{
    string matchingString  = conLines.FirstOrDefault(r => 
                              (r.IndexOf(item.firstParam)>= 0 && 
                              r.IndexOf(item.secondParam) >= 0) &&
                              (r.IndexOf(item.firstParam) < 
                               r.IndexOf(item.secondParam)));

    if(matchingString != null)
    {
        //CallYourMethod(matchingString);
    }
}
foreach(变量项第一次重复)
{
字符串匹配字符串=conLines.FirstOrDefault(r=>
(r.IndexOf(item.firstParam)>=0&&
r、 IndexOf(item.secondParam)>=0)&&
(r.IndexOf(第一参数项)<
r、 指数(第二项参数));
if(匹配字符串!=null)
{
//CallYourMethod(匹配字符串);
}
}
此代码:

  • 循环所有要检查的字符串
  • 在空格上拆分字符串,删除空元素
  • 查找
    firstDuplicate
    列表中参数与输入字符串匹配的所有条目
  • 将它们添加到结果
    foundreplicates
您必须注意:

  • conLines中字符串的有效性
  • 区分大小写
  • 处理每个字符串的多个结果和匹配多个字符串的条目

var foundDuplicates=new List();
foreach(var组合到conLines中查找)
{

//“它们将被至少一个空间隔开。” var parameters=combinationToFind.Split(新[]{''}, StringSplitOptions.RemoveEmptyEntries); var duplicatesForCombination=firstDuplicate.Where(d=> d、 firstParam==参数[0] &&d.secondParam==参数[1]); foundDuplicates.AddRange(duplicatesForCombination); }

现在您可以为
foundDuplicates

中的每个条目调用方法,因此,conLine是firstParam和secondParam连接的,比如conLine=firstParam+secondParam?您的问题是“如何确定字符串同时包含firstParam和secondParam”?一个字符串如何包含两个字符串,它们是使用分隔符连接的吗?你在浏览列表时有困难吗?你知道林克吗?他们至少会被一个空间隔开。我不太了解Linq。要清楚:您可以使用
firstParam=“foo1”
secondParam=“bar2”
和包含
“foo1 bar2”
conLines
创建一个
fixFirstDuplicate
,然后您想返回该
fixFirstDuplicate
实例吗?对于
“bar2 foo1”
“foo1 foo1”
,您不能使用基于哈希的集执行此操作。您将为该查询返回的每个结果调用该方法。我认为顺序无关紧要:“每次字符串同时包含firstParam和secondParam”。如果有,您可以修改为
&&conLines.IndexOf(r.firstParam)
,但我们不知道。当OP的问题很容易理解时,你不应该草草记下脑海中的第一个答案。首先使用评论要求澄清。您的编辑仍然不符合子字符串的情况。@user2340818,在这种情况下,您可以使用循环,我已修改了答案。@user2340818,我已再次修改了答案,您将从
ConLines
var foundDuplicates = new List<fixFirstDuplicate>();

foreach (var combinationToFind in conLines)
{
    // "They will be separated by at least one space."
    var parameters = combinationToFind.Split(new[] { ' ' },                  
                         StringSplitOptions.RemoveEmptyEntries);

    var duplicatesForCombination = firstDuplicate.Where(d => 
                                                d.firstParam == parameters[0]
                                            && d.secondParam == parameters[1]);

    foundDuplicates.AddRange(duplicatesForCombination);
}