- Back to Home »
- LINQ »
- LINQ TUTORIAL PART 3 - LINQ to OBJECTS
Posted by :
Sudhir Chekuri
Tuesday, 27 October 2015
LINQ to Objects queries will return data of IEnumerable type.
Example of LINQ to Object with array type:
using System;
using System.Linq;
namespace LINQApp
{
class Program
{
static void Main(string[] args)
{//Fruit names are created in an array object
string[] fruits = { "Guava", "Orange", "Apple", "Mango", "Banana" };
//All items from fruits array are queried using LINQ query
var items = from f in fruits
select f;
foreach (string s in items)
{
Console.Write(s + Environment.NewLine);
}
Console.ReadKey();
}
}
}
Output:
Guava
Orange
Apple
Mango
Banana
Example of LINQ to Objects using Custom type:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQApp
{
//Type with two properties Name and Password
class StudentLogin
{
public string StudentName { get; set; }
public string StudentPassword { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Student Object of List with StudentLogin type
List<StudentLogin> Student = new List<StudentLogin>();
//Adding 3 items to Student object
Student.Add(new StudentLogin { StudentName="Sudhir",StudentPassword="1234"});
Student.Add(new StudentLogin { StudentName = "Ram", StudentPassword = "Ramu" });
Student.Add(new StudentLogin { StudentName = "Raj", StudentPassword = "Raju" });
//Querying all items in Student Object
var stud = from st in Student
select st;
//Printing Name and passwords retreived using LINQ query
foreach (var s in stud)
{
Console.WriteLine(s.StudentName +" , "+s.StudentPassword);
}
Console.ReadKey();
}
}
}
Example of LINQ to Object with array type:
using System;
using System.Linq;
namespace LINQApp
{
class Program
{
static void Main(string[] args)
{//Fruit names are created in an array object
string[] fruits = { "Guava", "Orange", "Apple", "Mango", "Banana" };
//All items from fruits array are queried using LINQ query
var items = from f in fruits
select f;
foreach (string s in items)
{
Console.Write(s + Environment.NewLine);
}
Console.ReadKey();
}
}
}
Output:
Guava
Orange
Apple
Mango
Banana
Example of LINQ to Objects using Custom type:
using System;
using System.Collections.Generic;
using System.Linq;
namespace LINQApp
{
//Type with two properties Name and Password
class StudentLogin
{
public string StudentName { get; set; }
public string StudentPassword { get; set; }
}
class Program
{
static void Main(string[] args)
{
//Student Object of List with StudentLogin type
List<StudentLogin> Student = new List<StudentLogin>();
//Adding 3 items to Student object
Student.Add(new StudentLogin { StudentName="Sudhir",StudentPassword="1234"});
Student.Add(new StudentLogin { StudentName = "Ram", StudentPassword = "Ramu" });
Student.Add(new StudentLogin { StudentName = "Raj", StudentPassword = "Raju" });
//Querying all items in Student Object
var stud = from st in Student
select st;
//Printing Name and passwords retreived using LINQ query
foreach (var s in stud)
{
Console.WriteLine(s.StudentName +" , "+s.StudentPassword);
}
Console.ReadKey();
}
}
}
Output:
Sudhir , 1234
Ram , Ramu
Raj , Raju