简体   繁体   中英

Enumerable.Last method

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() .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM