Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/visual-studio-2010/4.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#_Visual Studio 2010_List_Indexing_Match - Fatal编程技术网

C# 如何找到第一个匹配项的索引?

C# 如何找到第一个匹配项的索引?,c#,visual-studio-2010,list,indexing,match,C#,Visual Studio 2010,List,Indexing,Match,我用这个: foreach (var item in set) { string matchingString = conLines.FirstOrDefault(r => (r.IndexOf(item.firstParam) >= 0 && r.IndexOf(item.secondParam) >= 0) && (r.IndexOf(item.firstParam) < r.IndexOf(item.secondParam))

我用这个:

foreach (var item in set)
{
    string matchingString = conLines.FirstOrDefault(r => (r.IndexOf(item.firstParam) >= 0 && r.IndexOf(item.secondParam) >= 0) && (r.IndexOf(item.firstParam) < r.IndexOf(item.secondParam)));
}
第一个参数,第二个参数包括:

ramp, table
article, letter
我所做的更改是在匹配中添加-01,我将打印出:

alpha beta dog cat
char ramp-01 table-01 seat
blog journal article-01 letter-01

不管你如何找到索引,它都是次优的。最后将枚举集合两次。相反,您应该:

  • 使用
    for
    循环来循环集合(如果可能)。这样,只要找到第一个匹配项,就已经有了索引(仅当集合公开和索引器以及预先计算的长度/计数属性时才有效):


  • 无论哪种方式,您都只需在集合上循环一次而不是两次(一次用于
    FirstOrDefault
    ,然后再循环一次以获取索引。

    无论您如何找到索引,它都将是次优的。您将枚举集合两次。相反,您应该:

  • 使用
    for
    循环来循环集合(如果可能的话)。这样,只要找到第一个匹配项,就已经有了索引(仅当集合公开和索引器以及预先计算的长度/计数属性时才有效):


  • 无论哪种方式,您都只需在集合上循环一次而不是两次(一次用于
    FirstOrDefault
    ,然后再循环一次以获取索引。

    您可以使用前面的
    选择
    ,其中包括索引:

    var temp =  conLines.Select((l, i) => new {l, i})
                        .FirstOrDefault(r => (r.l.IndexOf(item.firstParam) >= 0 
                                           && r.l.IndexOf(item.secondParam) >= 0)
                                           && (r.l.IndexOf(item.firstParam) < r.l.IndexOf(item.secondParam)
                                       ));
    string matchingString = temp.l;
    int index = temp.i;
    
    var temp=conLines.Select((l,i)=>new{l,i})
    .FirstOrDefault(r=>(r.l.IndexOf(item.firstParam)>=0
    &&r.l.IndexOf(第二项参数)>=0)
    &&(r.l.IndexOf(第1项参数)

    虽然我正在努力弄清楚如何获得想要的输出。为什么两行都有一个“01”?

    您可以使用前面的
    选择
    ,其中包括索引:

    var temp =  conLines.Select((l, i) => new {l, i})
                        .FirstOrDefault(r => (r.l.IndexOf(item.firstParam) >= 0 
                                           && r.l.IndexOf(item.secondParam) >= 0)
                                           && (r.l.IndexOf(item.firstParam) < r.l.IndexOf(item.secondParam)
                                       ));
    string matchingString = temp.l;
    int index = temp.i;
    
    var temp=conLines.Select((l,i)=>new{l,i})
    .FirstOrDefault(r=>(r.l.IndexOf(item.firstParam)>=0
    &&r.l.IndexOf(第二项参数)>=0)
    &&(r.l.IndexOf(第1项参数)

    虽然我正在努力弄清楚你是如何得到你想要的输出的。为什么两行都有一个“01”?

    如果你有时间并且不介意,你能告诉我你的意思吗?@user2340818-添加了示例。在第二个示例中你选择不使用foreach循环有什么原因吗?@sWW-Nope。这同样有效。同样的事情会发生吗在场景后面。如果你有时间并且不介意的话,你能告诉我你的意思吗?@user2340818-添加了示例。你在第二个示例中选择不使用foreach循环有什么原因吗?@sWW-没有。那也行。在场景后面做同样的事情。输出有点不相关。我将在字符串上执行一个方法找到匹配项,但方法(在本例中,将-01添加到匹配项中)对我的问题不重要。输出有点不相关。我将对找到匹配项的字符串执行方法,但方法(在本例中,将-01添加到匹配项中)对我的问题不重要。
    alpha beta dog cat
    char ramp-01 table-01 seat
    blog journal article-01 letter-01
    
    for(var i = 0; i < collection.Count; i++)
    {
        if(collection[i] == query)
        {
            // You have the match with collection[i] and the index in i
        }
    }
    
    var enumerator = collection.GetEnumerator();
    var count = 0;
    
    while(enumerator.MoveNext())
    {
        if(enumerator.Current == query)
        {
            // You have the match in enumerator.Current and the index in count
        }
        count++;
    }
    
    var temp =  conLines.Select((l, i) => new {l, i})
                        .FirstOrDefault(r => (r.l.IndexOf(item.firstParam) >= 0 
                                           && r.l.IndexOf(item.secondParam) >= 0)
                                           && (r.l.IndexOf(item.firstParam) < r.l.IndexOf(item.secondParam)
                                       ));
    string matchingString = temp.l;
    int index = temp.i;