Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Understanding LINQ Select and SelectMany operators in C# with examples

In this article, I am going to explain you the Language Integrated Query (LINQ)- select and SelectMany operators with examples. Select and SelectMany are the projection operators.

LINQ Select Operator

1) It is used to select values from collection.
2) Select Projects each element of a sequence into a new form.

LINQ SelectMany Operator

1) It select values from collection of collection (multiple collection).
2) Projects each element of a sequence to an IEnumerable and flattens the resulting sequences into one sequence.

Let us understand select and SelectMany with an example.

Select Example

Consider a Student class with 3 properties as shown below. We require two foreach loops to iterate through the studentResult, because query returns a collection of arrays.


namespace ConsoleApplication1
{
class Student
{
public int Id { get; set; }
public string Name { get; set; }
public Liststring> FavSubject { get; set; }
}
class Program
{
static void Main(string[] args)
{
List student = new List();

Student student1 = new Student { Name = "Rahul", FavSubject = new Liststring> { "PHP", "Java", "CSS" } };
Student student2 = new Student { Name = "John", FavSubject = new Liststring> { "C", "C++", "HTML" } };
Student student3 = new Student { Name = "Mary", FavSubject = new Liststring> { "C#", "C", "SQL", "Oracle" } };
student.Add(student1);
student.Add(student2);
student.Add(student3);
// using Select Query
IEnumerable> studentResult = student.Select(x => x.FavSubject);
//We require two foreach loops to iterate through the studentResult.
//Because query returns a collection of arrays.
foreach (List list in studentResult)
{
foreach (string sub in list)
{
Console.WriteLine(sub);
}
Console.WriteLine();
}
Console.ReadKey();
}
}
}
/*
OUTPUT:
PHP
Java
CSS

C
C++
HTML

C#
C
SQL
Oracle
*/

SelectMany Example

We require only one foreach loop to iterate through the studentResult, because query returns a one-dimensional collection.


// using SelectMany Query
IEnumerablestring> studentResult2 = student.SelectMany(x => x.FavSubject);

// We require only one foreach loop to iterate through the studentResult.
// Because query returns a one-dimensional collection.
foreach (string sub in studentResult2)
{
Console.WriteLine(sub);
}
Console.ReadKey();
/*
OUTPUT:
PHP
Java
CSS
C
C++
HTML
C#
C
SQL
Oracle
*/

LINQ to SQL : Writing Select Query

Below is the LINQ query which will fetch student details from database table tbl_Student. We need to create StudentData.dbml. Below is link for the SQL to LINQ tutorial.

What is LINQ to SQL and how to create LINQ to SQL class in C# with example for beginners

StudentDataContext dbContext = new StudentDataContext();
var students = from st in dbContext.tbl_Students
select new
{
st.Id,
st.Name,
st.Subject
};
grdStudents.DataSource = students;
grdStudents.DataBind();

Using LINQ Select to find lengths of each string in the array


static void Main(string[] args)
{
string[] SubjectArray = { "PHP", "Java", "CSS", "C", "C++", "HTML", "C#", "C", "SQL", "Oracle" };
IEnumerableint> length = SubjectArray.Select(x => x.Length);
foreach (int l in length)
{
Console.WriteLine(l);
}
Console.ReadKey();
}
/*
OUTPUT:
3
4
3
1
3
4
2
1
3
6
*/

Using LINQ project to a sequence of anonymous objects


static void Main(string[] args)
{
string[] SubjectArray = { "PHP", "Java", "CSS", "C", "C++", "HTML", "C#", "C", "SQL", "Oracle" };
var objects = SubjectArray.Select(x => new { Name = x, Length = x.Length });
foreach (var o in objects)
{
Console.WriteLine("Subject Name: {0}, Length: {1}", o.Name, o.Length);
}
Console.ReadKey();
}
/*
OUTPUT:
Subject Name: PHP, Length: 3
Subject Name: Java, Length: 4
Subject Name: CSS, Length: 3
Subject Name: C, Length: 1
Subject Name: C++, Length: 3
Subject Name: HTML, Length: 4
Subject Name: C#, Length: 2
Subject Name: C, Length: 1
Subject Name: SQL, Length: 3
Subject Name: Oracle, Length: 6
*/

Using LINQ for different concrete objects


class Program
{
static void Main(string[] args)
{
string[] SubjectArray = { "Php", "Java", "Css", "C", "C++", "HtMl", "C#", "C", "sQL", "Oracle" };

IEnumerable studentList = SubjectArray.Select((x, i) => new Student() {
CapitalLetters = x.ToUpper(), SubIndex = i + 1, Subject = x, SubLength = x.Length });
foreach (Student s in studentList)
{
Console.WriteLine(string.Concat(s.SubIndex, ": ", s.Subject, ", ", s.SubLength, ", ", s.CapitalLetters));
}
Console.ReadKey();
}
}
public class Student
{
public string Subject { get; set; }
public int SubLength { get; set; }
public string CapitalLetters { get; set; }
public int SubIndex { get; set; }
}
/*
OUTPUT:
1: Php, 3, PHP
2: Java, 4, Java
3: Css, 3, CSS
4: C, 1, C
5: C++, 3, C++
6: HtMl, 4, HTML
7: C#, 2, C#
8: C, 1, C
9: sQL, 3, SQL
10: Oracle, 6, Oracle
*/

Using LINQ Select to replace substrings


static void Main(string[] args)
{
string[] SubjectArray = { "PHP", "Java", "CSS", "C", "C++", "HTML", "C#", "C", "SQL", "Oracle" };

IEnumerablestring> SubjectReplace = SubjectArray.Select(x => x.Replace("C", "J"));

foreach (string subject in SubjectReplace)
{
Console.WriteLine(subject);
}
Console.ReadKey();
}
/*
OUTPUT:
PHP
Java
CSS
J
J++
HTML
J#
J
SQL
Oracle
*/

Using LINQ SelectMany to convert each string in the string array to a character array


static void Main(string[] args)
{
string[] SubjectArray = { "PHP", "Java", "CSS" };

var result = SubjectArray.SelectMany(x => x.ToCharArray());

foreach (char letter in result)
{
Console.WriteLine(letter);
}
Console.ReadKey();
}
/*
OUTPUT:
P
H
P
J
a
v
a
C
S
S
*/


This post first appeared on ASPArticles, please read the originial post: here

Share the post

Understanding LINQ Select and SelectMany operators in C# with examples

×

Subscribe to Asparticles

Get updates delivered right to your inbox!

Thank you for your subscription

×