0

I have been looking for a long time now, to find any smarter solution than mine, to gather index and value of an item in my array. I can not search directly on the item because there will always be some other char in the string. Here is an example of what I want to do.

// logcontent = ["f", "red", "frs", "xyr", "frefff", "xdd", "to"]
string lineData = "";
int lineIndex = 0;
foreach (var item in logContent.Select((value, index) => new { index, value }))
{
    string line = item.value;
    var index = item.index;

    if (line.Contains("x"))
    {
        lineData = line;
        lineIndex = index; 
        break;
    }
}

I want to only get the next item

lineData = "xyr";
lineIndex = 3;
4
  • 1
    Please clarify what you want to do. Do you want to have all Indexes of lines containing an 'x' ? Commented Nov 8, 2017 at 7:27
  • Updated, hope it is more understanable now Commented Nov 8, 2017 at 7:30
  • @TobiasMøller - instead of editing your question to include the answer better just mark that answer with the V :) it is clearer for future readers Commented Nov 8, 2017 at 7:38
  • Okay changed back to normal post. Thank you :) Commented Nov 8, 2017 at 7:39

4 Answers 4

2

Use linq's FirstOrDefault:

var result = logContent.Select((value, index) => new { index, value })
                       .FirstOrDefault(item => item.value.Contains("x"));

If there is no such item you will get null.

If using C# 7.0 you can use named tuples:

(int lineIndex, string lineData) = logcontent.Select((value, index) => (index, value))
                                             .FirstOrDefault(item => item.value.Contains("x"));

and then o something with lineIndex or lineData directly which is like you would with the original version

Sign up to request clarification or add additional context in comments.

2 Comments

@MongZhu - I did not claim that the linq is faster that the loop. I claimed that it does not have to iterate the collection twice. deffered execution returns each time only the one next item matching the passed function, which is passed to the next method in the chain. Therefore in the case above if the item with the x in the value is first then the Select will create the object for it with the index and pass it for the FirstOrDefault which will return it.
@GiladGreen thank you for the explanation. Up to now I found also enough articles that describe this part, and I understood it. I already upvoted your answer, so thanx again for the lesson. Peace out
1

If i understood the Question correct you want to have the next string containing an "x" in a string array.

 var result = logContent.Select((value, index) => new { index, value })
                        .First(x => x.value.Contains("x");

Comments

1

Here is another approach with Array.FindIndex - which performs better than the Linq version

string[] logContent = { "f", "red", "frs", "xyr", "frefff", "xdd", "to" };  

int lineIndex  = Array.FindIndex(logContent, x => x.Contains("x"));
string lineData = lineIndex >= 0 ? logContent[lineIndex] : null;

Comments

0

this code

string[] ogcontent = {"f", "red", "frs", "xyr", "frefff", "xdd", "to"};
    string lineData = "";
    int lineIndex = 0;
    for (int i = 0; i < ogcontent.Length; i++)
    {
        string line = ogcontent[i];
        var index = i;

        if (line.Contains("x"))
        {
            lineData = line;
            lineIndex = index; 
            Console.WriteLine("index = {0}", i);
            Console.WriteLine("value = {0}", line);
            break;
        }
    }

result

index = 3
value = xyr

working sample

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.