How to read characters from a string using LinQ in C#?

Introduction:

In this article i will explain how to query for characters in a string using LinQ in C#

Description:

In previous articles i explained How to replace string in C#How to convert string to int in C#, and How to read characters from string. In this article i will explain how to query for characters in a string using LinQ in C#.

Example:

string Email = "vikram";

Now read all characters from Email string by using LinQ.

// insert all characters into Array.
var characters = from c in Email
                 select c;

Now display each character from characters array in new line by using foreach.

foreach (char c in characters)
{
    // Display character by character.
    Console.WriteLine(" " + c + "\n");
}

Full Source Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpPractice
{
    class QueryAsString
    {
        public static void Main()
        {
            string Email = "vikram";
            // insert all characters into  Array.
            var characters = from c in Email
                             select c;
            Console.WriteLine(" Characters: ");
            foreach (char c in characters)
            {
                // Display character by character.
                Console.WriteLine(" " + c + "\n");
            }
            Console.ReadLine();
        }
    }
}

Output: