【问题标题】:How to search through two list members?如何搜索两个列表成员?
【发布时间】:2014-06-26 18:23:19
【问题描述】:

我有以下课程:

    public class fixFirstDuplicate
    {
        public string firstParam { get; set; }
        public string secondParam { get; set; }
    }

还有名单:

public static List<fixFirstDuplicate> firstDuplicate = new List<fixFirstDuplicate>();

列表中充满了 firstParam 和 secondParam 的值,它们彼此对应。它基本上是一个表,其中第一个 firstParam 与第一个 secondParam 相关联,依此类推。

那么我有:

List<string> conLines = new List<string>();

我想查看 conLines,并且每次一个字符串同时包含 firstParam 和 secondParam 时,对该字符串执行一个方法。

如果我只使用一个列表,比如 firstParam,我会使用 HashSet,但我不知道如何做等效的操作,因为我有 firstParam 和 secondParam

【问题讨论】:

  • 所以,conLines 是 firstParam 和 secondParam 连接的,比如 conLine = firstParam + secondParam?
  • 你的问题是“如何确定一个字符串同时包含firstParam和secondParam”?一个字符串如何包含两个字符串,它们是否使用分隔符连接起来?您在遍历列表时遇到问题吗?你知道 Linq 吗?
  • 它们至少用一个空格分隔。我不太了解 Linq。
  • 要明确:你可以有一个fixFirstDuplicatefirstParam = "foo1"secondParam = "bar2" 和你的conLines 包含"foo1 bar2",然后你想返回那个fixFirstDuplicate 实例?而不是"bar2 foo1" 也不是"foo1 foo1"
  • 您无法使用基于哈希的集合来执行此操作。

标签: c# string visual-studio-2010 list hashset


【解决方案1】:

您需要检查conLines 是否同时包含firstParamsecondParam,例如:

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);
    }
}

【讨论】:

  • 您将为该查询返回的每个结果调用该方法。
  • 我认为顺序并不重要:“每次字符串都包含 firstParam 和 secondParam”。如果是这样,您可以修改为 &amp;&amp; conLines.IndexOf(r.firstParam) &lt; conLines.IndexOf(r.secondParam)
  • 但我们不知道。当 OP 的问题可以广泛解释时,您不应该记下想到的第一个答案。使用 cmets 先要求澄清。而且您的编辑仍然不适合子字符串的情况。
  • @user2340818,这样的话你可以使用循环,我已经修改了答案。
  • @user2340818,我再次修改了它,而不是索引你将从ConLines获取字符串
【解决方案2】:

这段代码:

  • 遍历所有要检查的字符串
  • 在空格上拆分字符串,删除空元素
  • firstDuplicate 列表中查找所有参数与输入字符串匹配的条目
  • 将它们添加到结果中foundDuplicates

你必须照顾好:

  • 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);
}

现在您可以为foundDuplicates 中的每个条目调用您的方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-03
    • 1970-01-01
    • 1970-01-01
    • 2012-10-24
    相关资源
    最近更新 更多