0% found this document useful (0 votes)
91 views28 pages

LINQ To Object

By this document, i have showed how we can work on LINQ to Object functionalities. Just read this document if you want to start working before going in deep.

Uploaded by

Foyzul Karim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views28 pages

LINQ To Object

By this document, i have showed how we can work on LINQ to Object functionalities. Just read this document if you want to start working before going in deep.

Uploaded by

Foyzul Karim
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Foyzul Karim

LINQ to Object
Why a businessman needs
software?
To automate his business (data)
Where does the data stored?

Database, RAM, XML, File etc


Working with Database

• SqlConnection
• Query
• Execution procedure etc
Working with ADO.NET

• DataTable
• DataSet 
• SqlDataAdapter etc
Working with RAM data

• Create user defined class


• Load its properties with data 
• Arrays etc
Whats the point?

Different working procedure with different


syntax
             Find out the differences

            var query = from             var query = from


studentObject in students studentObject in db.students
            select studentObject;             select studentObject;
             Find out the differences

            var query = from             var query = from


studentObject in students studentObject in db.students
            select studentObject;             select studentObject;

The left query is used to select data from a list of students


and the right query is used to retrieve data from student
table of a database. But the syntax is almost same. 

Amazing. Isn't it?


LINQ

Language Integrated Query


How it works?

In simplified english:
from variable in source

In syntax:
from customerObject in Customers
How it works?

In simplified english:
from variable in source
where the condition is true

In syntax:
from customerObject in Customers
where customerObject.Name == "Foysal"
How it works?

In simplified english:
from variable in source
where the condition is true
select data

In syntax:
from customerObject in Customers
where customerObject.Name == "Foysal"
select customerObject;
Types of LINQ

• LINQ to Object
• LINQ to SQL
• LINQ to XML
Types of LINQ

• LINQ to Object (Today's agenda)


• LINQ to SQL
• LINQ to XML
LINQ To Object

 
 Initialization Expression:

By using an object initialization expression, we can


initialize an object without calling its constructor and not
setting its properties.

Standard object initialization Initialization expression

Student student=new Student(); Student student = new Student


student.Id = "001"; {Id = "001", Name = "Foysal",
student.Name = "Foysal"; Address = "Dhaka"};
student.Address = "Dhaka";
 Anonymous types

By using the anonymous type, we don't have to create a


prototype (or we can call it as a class) of our required
data out of many of them.

Example:
new {studentObject.Name,studentObject.Address};

The compiler will create a class for our selected values.


Restriction Operator : Where
This operator allows us to filter our required data from a
collection of data.

Example:

var selectedNames = from name in names


                                    where name.Length == 6
                                    select name;

or

var selectedStudents = from student in students


                                    where student.Name.Length == 6
                                       select student;
 

Practice:
1. Get the student list who live in Shamoli.
2. Get the names which has the length of more than 6
How to work well in .NET

To work better with a tool, you must know what can you
do with that tool. Remember, Language (C# for
example) is just a tool for your coding. So, you have to
know what can you do with this tool.

Linq provides a lot of methods to work on. I am giving


another example of restriction operator in the upcoming
slide.
  Restriction Operator : Where

Example:
var selectedNames =  from name in names
                                      where name.EndsWith("l")
                                      select name;

or

var selectedStudents = from student in students


                               where student.Name.StartsWith("S")
                                       select student;
 

Practice:
1. Find the names which contains the string sequence
of "on"
2. Find the student list who has address containing the
string sequence of "li"
 Projection Operator: Select
By this operator, we select our data in different ways.
Example:
var selectedNames = from name in names
                        where name.Contains("Foysal")
                        select name;

Or,

var selectedStudents = from student in students


                               where student.Department=="CSE"
                                       select student.Name;
 Projection Operator: Select (Cont.)
Example:
var selectedStudents = from student in students
                               from course in student.CourseTaken
                    where course.CourseName.StartsWith("C")
                                  select student;
Or,
var selectedStudents = from student in students
                              from course in student.CourseTaken
                   where course.CourseName.StartsWith("C")
           select new {student.Name, course.CourseName};
 

Practice:
1. Find the courses which are taken by the students who lives
in Shamoli.
2. Find the course Credit of the courses which are taken by the
students whose name are 6 character long.
What else we can do with it?
There are several more operator in LINQ. Such as:
• Partitioning Operators
• Ordering Operators 
• Grouping Operators 
• Set Operators 
• Conversion Operators 
• Element Operators 
• Generation Operators 
• Quantifiers
• Aggregate Operators 
• Miscellaneous Operators
• Custom Sequence Operators
• Query Execution
• Join Operators
• Utility Routines
Next Day: “LINQ to SQL”

You might also like