{LINQ Samples} Sample 4 - Search a collection of Business Objects

Note: These LINQ sample codes are written keeping the beginner in mind so the code is made as simple as possible.

LINQ Samples series intro: LINQ-Language INtegrated Query is a great .Net language feature, to simply put, it allows you to query data objects in the way you query database tables using english sentence like syntax.

If you are unfamiliar with business objects please read this classic article by Scott Mitchell. If you are unfamiliar with the object and collection initializer syntax (new Student(){StudentId=1,StudentName="Arnold"}), please read this post about object and collection initializers.

We will be using the below Student class and a collection of Student objects in this sample, I am using a generic List(List<T>), generics are not present in C#/.Net framework when Scott wrote the above article.

    
class Student
{
    public int StudentId { get; set; }
    public string StudentName { get; set; }
}

In this sample we will search a collection of Student objects.

//Initialize student list for our sample..
List<Student> studentList = new List<Student>() 
                    { 
                        new Student(){StudentId=1,StudentName="Arnold",BloodGroup="A+"},
                        new Student(){StudentId=2,StudentName="Matt",BloodGroup="B+"},
                        new Student(){StudentId=3,StudentName="Brad",BloodGroup="B+"},
                        new Student(){StudentId=4,StudentName="Tom",BloodGroup="B-"}
                    };


//Remember 'var' can also be used..
//var slist                = from s in studentList -- 
IEnumerable<Student> slist = from s in studentList
                                where s.BloodGroup == "B+"
                                select s;

foreach (var s in slist)
{
    Console.WriteLine(s.StudentName);
}

Output:

Matt

Brad



Here first s transforms itself as an item in the studentList collection, then the where section uses the range variable s to filter the collection to get the final result.

Bookmark / Share

{LINQ Samples} Sample 3 - Display items in a string array that starts with a particular character or string

Note: These LINQ samples are written keeping the beginner in mind so the code is made as simple as possible.

LINQ Samples series intro: LINQ-Language INtegrated Query is a great .Net language feature, to simple put, it allows you to query data objects in the way you query database tables using english sentence like syntax.

string[] strArray = new string[] { "plum", "grape", "pineapple", "lemon" };

IEnumerable s = from x in strArray
                where x.StartsWith("p")
                select x;
                //Can use other functions like x.Contains() to get matching elements that contains the specified value..
Console.WriteLine(s);

Output:

plum

pineapple

 

First “x” transforms itself as an item in the strArray and the LINQ code runs for each element in strArray, the “where” section is used to specify the condition, which here uses the x(an element of the array) and “select” section is where the final output of the LINQ query is returned, here it is the array elements.

Bookmark / Share

{LINQ Samples} Sample 2 - Display list of items in a string array based on item(element) length

Note: These LINQ samples are written keeping the beginner in mind so the code is made as simple as possible.

LINQ Samples series intro: LINQ-Language INtegrated Query is a great .Net language feature, to simple put, it allows you to query data objects in the way you query database tables using english sentence like syntax.

 

string[] strArray = new string[] { "apple", "orange", "grape", "lemon" };


IEnumerable s = from x in strArray 
                        where x.Length == 6 
                        select x; 
Console.WriteLine(s);

 

Output:

orange

 

First “x” transforms itself as an item in the strArray and the LINQ code runs for each element in strArray, the “where” section is used to specify the condition, which here uses the x(an element of the array) and “select” section is where the final output of the LINQ query is returned, here it is the array elements.

Bookmark / Share

{LINQ Samples} Sample 1 - Search a string array using LINQ

Note: These LINQ sample codes are written keeping the beginner in mind so the code is made as simple as possible.

LINQ Samples series intro: LINQ-Language INtegrated Query is a great .Net language feature, to simple put, it allows you to query data objects in the way you query database tables using english sentence like syntax.

 

string[] strArray = new string[] { "apple", "orange", "grape", "lemon" };

IEnumerable s = from x in strArray 
                        where x.Contains("ap") 
                        select x; 
Console.WriteLine(s);

Output:

apple

grape

 

As this is the first LINQ sample I am explaining a little bit about how the code executes internally.

Here “x” transforms itself as an item in the strArray and the LINQ code runs for each element in strArray, the “where” section is used to specify the condition, which here uses the x(an element of the array) and “select” section is where the final output of the LINQ query is returned, here it is the array elements.

Bookmark / Share