none
display list of words by their first letter using linq RRS feed

All replies

  •  public static string ListByFirst(IEnumerable<string> words)
            {
                return string.Join("\r\n", words.OrderBy(f => f[0]));
            }


    Ethan Strauss

    Tuesday, July 17, 2018 6:10 PM
  • https://www.dotnetperls.com/substring

    If it's a string object then you would use the substring command.

    var results = (from a in list select a);

    foreach(var str in results)

    {

         string somestr = str.substring(0,1);

    }

    It's one way to do it.

    Tuesday, July 17, 2018 6:12 PM
  •   

    Hi Fani_Prizmic,

    Thank you for posting here.

    >>display list of words by their first letter using linq 

    What do you mean that "display list"? I guess you want to sort the records by the first letter, if so,  please consider the code below.

            static void Main(string[] args)
            {
                string names = "Li;Zhang;Wang;Zhao;An";
                List<string> nameList = names.Split(';').OrderBy(name => name).ToList();
                foreach (var str in nameList)
                    Console.WriteLine(str);
            }
    

    Please let me know if I misunderstand what you want, and provide more details for your question.

    Best Regards,

    Wendy


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.



    Wednesday, July 18, 2018 5:33 AM
    Moderator
  • Hi,

    Do you mean filtering the list of the words by their first character/letter or displaying the first letter?

    Wednesday, July 18, 2018 4:36 PM