In Focus
Jabalpur Chapter Meet July 13: Register Now
Chandigarh Chapter: Developers Day on 29 June 2013
Delhi Chapter Hands On June 22: Limited Seats Register Now
Email :
Password :
Remember me?
Forgot password
Contribute
An Article
A Blog
A News
A Video
A Link
An Interview Question
Ask a Question
TECHNOLOGIES
.NET 4.5
Chapters
JavaScript, CSS
SharePoint 2010
Web Development
.NET Assemblies
Coding Best Practices
JQuery
SharePoint 2013
Web Services in C#
ADO.NET in C#
Current Affairs
JSP
Silverlight with C#
Windows 8
AJAX in C#
Databases & DBA
Leadership
Smart Devices
Windows Azure
Android Programming
Design & Architecture
Learn .NET
SQL
Windows Controls C#
Articles C#
Exception Handling C#
LightSwitch 2012
SQL Server 2012
Windows Forms C#
ASP.NET Controls in C#
Expression Studio
LINQ
Testing
Windows Phone 8
ASP.NET MVC with C#
Graphics Design
Mobile & Embedded
TypeScript
Windows Server 2012
ASP.NET Programming
How do I
Office 2013
Visual C#
Windows Store Apps
BizTalk Server
HTML 5
OOP/OOD
Visual Studio .NET
Workflow Foundation in C#
C# Language
Internet & Web
Operating Systems
Visual Studio 2010
WPF
C# Tutorials
iPhone/iPad
PHP
Visual Studio 2012
XAML
C, C++, MFC
Java
Products
WCF with C#
XML
Career Advice
Java and .NET
Project Management
Request a new Category
|
View All
ANSWERS
BLOGS
VIDEOS
INTERVIEWS
BOOKS
LINKS
NEWS
CHAPTERS
CAREER ADVICE
iOS 7 beta: Testers warned when using uncert ...
Design Simple Logo with help of Expression D ...
Credit Card Payment in ASP.Net using Stripe.com
Menu Style like jQuery
Why Constructor can't be declared Virtually? ...
Send Email in HTML Format using SQL database ...
Waiting Message in Window Application using C#
Merging Several Rows into a Single Row in SQ ...
Get Values from Client Side to Server Side u ...
How to Move a Folder to Another Folder with ...
Blog
74
1,529
LINQ - Creating Query like in SQL
Posted by
Mahadesh Mahalingappa
in
Blogs
|
LINQ
on
Sep 22, 2011
In this article we would learn how to use the Create Query Method in LINQ such that we can create SQL like queries .
Tweet
1814
0
0
In this article we would learn how to use the Create Query Method in LINQ such that we can create SQL like queries .
This would be particularly useful for those who are used to working in SQL and hence feel LINQ queries are alien to them .
The first step is create a new project lets say a Console Application . And add a ADO.Net Entity Data Model . if you are following my articles , I have explained this step so many times so from now on I will skip explaining this step.
Ok we have added the ADO.Net Entity Data Model . And our Data Model is now ready .
Now lets use the Context object created in the Data Model to create our Query.
The code in the Program class would like as below :
Entities
context =
new
Entities
();
Now lets create our Query :
Below is a simple query :
string
queryString =
@"
Select
VALUE
c
From
Entities. PRODUCT
As
c"
;
I have highlighted all the keywords . This is pretty easy actually . Once you get used to it .
The SQL version of this Query is :
Select * from PRODUCT
Okay so we have written our query . Now lets pass it to the CreateQuery() Method and get the Output .
The syntax to pass the Query is given below :
ObjectQuery
<
PRODUCT
> contactQuery = context.CreateQuery<
PRODUCT
>(queryString,
new
ObjectParameter
(
""
,
""
));
In our case there are no parameters to be passed hence I have just said new ObjectParameter("","") as the second parameter . Passing a Null would throw a error at runtime .
When we run the Program, contactQuery will now have all the data we need .
Similarly we could also implement the Joins using custom Query . The syntax for the custom Joins is given below :
string
queryString =
@"Select VALUE d.Company_ID
From Entities.PRODUCT As c
Join Entities.COMPANY As d
On d.Company_ID == c.Company_ID"
;
In this article we just saw how to implement SQL like queries in .Net using LINQ . Happy Coding
This Feature is Sponsored By
DynamicPDF Merger is a developers dream for interacting with any existing PDF documents. Merge, append, split, form fill, flatten stamp and so much more.
Using LINQ to XML to Query Data ..
Querying a DataSet with DLINQ
comments
View Previous Comments
of
COMMENT USING
PREMIUM SPONSORS
Nevron
Nevron Software is a global leader in component based data visualization technology for a diverse range of Microsoft centric platforms. Nevron Data Visualization components are used by many companies, educational and government organizations around the world.
More Blogs from this Blogger
Creating ViewModel in Silverlight
Creating a Multi Value Converters in Silverlight
Silverlight - Drawing a line Programatically
Enable Full Screen in Silverlight
Creating the Item Source for the Observable Collection in Silverlight
Creating a Observable Collection in Silverlight
Creating Glass Effects in Silverlight
Generic Join Method Using LINQ
Fetch by Primary Key using LINQ - Generic
Constructing Lambda in VB.Net
View All
SPONSORED BY
Get the industry leading .NET Charting component
.NET Charting component with full set of charting and gauge types.
Follow @twitterapi
Latest Blogs
Why Developers Should Read News
In Defence of Books
Media Player, Real Player, QuickTime Player in ASP.Net
Credit Card Payment in ASP.Net using Stripe.com
Crop Image in ASP.Net using Script
Creating Social Site Buttons By addthis.com in ASP.Net
Internals of C# Events
Design Simple Logo with help of Expression Design
How to get list of all folders in a Dropdown List
Search in Coma delimited string
View All
Poll
Result
All Polls
Speed of C# Corner
How do you find speed of C# Corner when visiting the site?
Very Fast
OK
Slow
Very Slow
How do you find speed of C# Corner when visiting the site?
Options
Votes
%
Very Fast
4
7.55
OK
42
79.25
Slow
4
7.55
Very Slow
3
5.66
Total
53
100%