Académique Documents
Professionnel Documents
Culture Documents
LINQ
IntelliSense
A query is a set of instruction that describes what data to retrieve from a given
data source (or sources) and what shape and organization the returned data should
have.
The first element that matches a condition, or the sum of particular values in a specified
set of elements
Must begin with a from clause and must end with a select or group clause.
Between the first from clause and the last select or group clause, it can contain
one or more of these optional clauses:
where, orderby, join, let and even additional from clauses. You can also use into keyword
to enable the result of a join or group clause to serve as the source for additional query
clauses in the same query expression.
Query Variable
In LINQ, a query variable is any variable that stores a query instead of the
results of a query.
Syntax
Query syntax
Method syntax
Query Syntax
Method Syntax
Method Syntax
Lambda expression
Read as goes to
Input variable
c => c > 15
Goes hand-in-hand with type inference, a new feature that allows local variables to
be automatically typed by their initialization expression.
In the New Project dialog box select the Visual C# Templates project type.
Provide a name for the new project by entering LINQ Practice in the Name
box.
Click OK.
Inside the NumberQuery function, write a LINQ query that filters the even
numbers from the original list you have created and assign it to a variable
named evenNumbers.
Inside the NumberQuery function write a console output code that displays
the result.
Display how many even numbers and how many odd numbers does exists in
the list.
Override the ToString() function of the class to return the StudentName and
the Address in a tab separated manner
Inside the Program class, add a new method with the following signature
static IEnumerable<Student> CreateStudents()
City
Tadios
Addis Ababa
Ezra
Arba Minch
Martha
Bishoftu
Mahelet
Adama
Sem
Addis Ababa
Zeritu
Bishoftu
Create a method inside the program class with a name ObjectQuery. The
method should have an implementation which query students that live in
Addis Ababa and output the result as a string on the console window.
Modify your query to display the first student that lives in Bishoftu.
Modify your query to display the first student that lives in Mekele.
Name
Id
CategoryId
Name
Id
Inside the Program class create two methods with the following signatures
static IEnumerable<Category> GetCategories()
static IEnumerable<Product> GetProducts()
Id
Cat. Id
Cat. Name
Id
Orange
Fruits
Banana
Vegetables
Carrots
Beverages
Cabbage
Beer
Milk
Create a method inside the program class with a name JoinQuery. The method
should have an implementation which query products together with their
category and output the result on the console window.
LINQ to SQL
LINQ to SQL
StudentId
INT
NOT NULL
FirstName
LastName
NVARCHAR(50)
NOT NULL
City
NVARCHAR(50)
NOT NULL
NVARCHAR(50)
NOT NULL
PRIMARY KEY
CourseId
INTEGER
NOT NULL
CourseName
NVARCHAR(50)
NOT NULL
CreditHours
INTEGER
PRIMARY KEY
NOT NULL
StudentId
INTEGER
NOT NULL
PRIMARY KEY
CourseId
INTEGER
NOT NULL
PRIMARY KEY
Term
NVARCHAR(50)
NOT NULL
PRIMARY KEY
Grade
NVARCHAR(2)
NOT NULL
Click OK.
Drag and drop all the tables to the HSIS.dbml designer interface.
Each tables can be accessed as a property for an instance of our designer code,
which is HSISDataContext.
The DataContext object used in the Student method is the main conduit through which
objects are retrieved from the database and changes are submitted.
Create a function called AddData and implement a logic that inserts a new
student in the student table.
Use the SubmitChanges method of the context to apply the update to the
database.
Create a function called UpdateData and implement a logic that updates the
city of the student named Ezra to Hawassa.
HINT: Use the SubmitChanges method of the context to apply the update to the
database.
HINT: Use the SubmitChanges method of the context to apply the delete operation
to the database.
QUESTION 1:
The function should display the customer id, contact name, and city with tab serparation for all customers
whose city is London.
QUESTION 2:
The function should display the average of unit price for products with a product name that begins with the
letter A.
QUESTION 3:
The function should display the name and unit price of all products whose product name contains the word
Ch or the unit price is below $5.
HINT: Contains
QUESTION 4:
The function should display the name and unit price of all products which are discontinued. Use anonymous
type to select only the product name and unit price.
QUESTION 5:
The function should display the first name and last name of employees whose birthday is in January regardless
of the year they are born in.
QUESTION 6:
The function should display the minimum price, the maximum price and the sum of all orders for all products in
the database.
QUESTION 7:
The function should display the contact name of all customers with at least one
order to their name.
HINT: Any
QUESTION 8:
The function should display the contact name of all customers whose all orders have
all had freight costs under 50.
HINT: All
Entity Framework
Allows you to create a model by writing code or using boxes and lines in the
EF Designer.
Entity Framework
We can have all data access logic written in higher level languages
The underlying data store can be replaced without much overhead since all data
access logic is present at a higher level.
Entity Framework
EF sits on top of ADO.NET. EF allows you to focus on writing the app rather
than writing a bunch of ADO.NET code.
Your code that is written using ADO.NET can be faster than EF. However, if you
write bad ADO.NET code, it will be slower than EF.
You really have to know what you are doing to make your ADO.NET code
faster than EF. EF performance is quite acceptable for most business
applications. EF is an Object Relational Mapper (ORM) framework similar to
Java's Hibernate.
Question
LINQ to SQL:
Entity Framework:
In .NET 3.5, LINQ to SQL had much better support for SQL-Server-specific
functionality than EF. This is mostly not true in .NET 4; theyre fairly similar in
that respect.
The EF let you choose Model First, DB First, or Code First Modeling. LINQ to
SQL, out of the box, really only supports DB First.
Select Data Source (If you havent connected to a database from Server
Explorer)
Select OK and you will be asked if you want to create a new database, select Yes
The new database will now appear in Server Explorer, right-click on it and
select New Query
Copy the following SQL into the new query, then right-click on the query and
select Execute
Select OK
Select the connection to the database you created in the first section,
enterBloggingContextas the name of the connection string and clickNext
Click the checkbox next to Tables to import all tables and click Finish
Once the reverse engineer process completes the new model is added to your
project and opened up for you to view in the Entity Framework Designer. An
App.config file has also been added to your project with the connection
details for the database.
Add an implementation to take blog name from user and add it to the database.
After adding it, display all blogs in the database on the console window. The result
might look like this
Copy the following SQL into the new query, then right-click on the query and
selectExecute
On the Add tab of the Update Wizard check the box next to Tables, this
indicates that we want to add any new tables from the schema.
Exercise II
The method has a sequence of blogs passed to it. Your code should use the bulk add
method provided by the entity framework data context.
From the console main function, call the method AddBlogs to add the following data to
the database.
Blog Name
URL
http://blogs.msdn.com/b/adonet/
Physics Blog
http://blog.physicsworld.com/
ADO.NET Blog
http://blogs.msdn.com/b/adonet/
Science Blog
http://scienceblogs.com/channel/environment/?
utm_source=globalChannel&utm_medium=link
IT Blog
http://cee.mit.edu/news/blogs
Exercise II
The method has a sequence of blogs passed to it. Your code should use the bulk remove
method provided by the entity framework data context.
From the console main function, call the method DeleteBlogs to remove all blogs with url
value equals http://blogs.msdn.com/b/adonet/.