-2

I am trying to print the both the value of .First and .Last but only the value of .First() comes up. .Last() just prints my "Text: " word and then blank.

I'v tried chainging the i to different values and that just makes the program worse.

case 2:
    Console.Write("\n\tType a word you want to search for: ");
    string sokord = Console.ReadLine();                           
    for (int i = 0; i < loggList.Count; i++)
    {
        if (loggList[i].First() == sokord)
        {
            Console.WriteLine("\n\tTitel: " + loggList[i].First() + "\n\tText: " + loggList[i].Last());                                       
            break;
        }
    }

I want to get the titel from .First() and the text from .Last().

Alexander Schmidt
  • 5,631
  • 4
  • 39
  • 79
ziNo
  • 7
  • 2
  • 3
    What type is `loggList`? – Sumner Evans Nov 07 '19 at 16:21
  • It is a string List. – ziNo Nov 07 '19 at 16:22
  • Are you changing the `List` while you iterating over it? I mean, can this happen? – Alexander Schmidt Nov 07 '19 at 16:23
  • 2
    @ziNo, so `loggList[i]` is a `string`... and you want first and last letter/char ? – demo Nov 07 '19 at 16:23
  • 1
    Seems like he is trying to find matching items > Change if (loggList[i].First() == sokord) this to if (loggList[i]== sokord) – N Subedi Nov 07 '19 at 16:24
  • 1
    @ziNo please show an example of what data you have as input in `loggList` and what data you want to be output. Right now, it is not clear what you want to happen. – Sumner Evans Nov 07 '19 at 16:25
  • I just lookup op "sokord", and translates to word/tag. So problably the OP wants to test on the first word. And print the first & last word, not the first and last characters. – GvS Nov 07 '19 at 16:27
  • Console.Write("\n\tPick a title: "); loggArray = new string[3]; loggArray[0] = Console.ReadLine(); Console.WriteLine("\n\Type your log: "); loggArray[1] = Console.ReadLine(); loggList.Add(loggArray) – ziNo Nov 07 '19 at 16:32

1 Answers1

0

This is very difficult to guess at, since there isn't enough information about the type of loggList in the question.

I would probably write the code as follow though:

var firstMatch = loggList.FirstOrDefault(l => l.First() == sokord);
Console.WriteLine("\n\tTitel: " + firstMatch?.First() + "\n\tText: " + firstMatch?.Last());
Naylor
  • 752
  • 7
  • 20