I am having problem while creating equvalent LINQ query for the SQL query.
I have folowing SQL query:
SELECT Cnt.Id, Cnt.Headline, Cnt.Body, Cnt.CreateDateTime, Cnt.ImageUrl, Sb.Name
FROM Contents AS Cnt INNER JOIN
SubCategories AS Sb ON Cnt.SubcategoryId = Sb.Id
WHERE (Sb.CategoryId = 1) AND (Cnt.Id =
(SELECT MAX(Id) AS Expr1
FROM Contents
WHERE (SubcategoryId = Sb.Id)))
GROUP BY Cnt.Id, Cnt.Headline, Cnt.Body, Cnt.CreateDateTime, Cnt.ImageUrl, Sb.Name
ORDER BY Cnt.CreateDateTime
Now I want to convert this query into its equvalent LINQ, but I not getting the way how this can be done.
Please tell me how this can be done.
I want to bind the result to a repeater control like below:
Not every SQL can be fully converted to LINQ. So I suggest you trying to manually bind the SQL in the form of a Stored Procdure and bind the result to your manually-created Entity model.
In fact you can first create a Stored Procdure,and then open "View"=>"Server Explorer" in the VS to drag and drop the Stored Procdure into the dbml file.
sophia_asp
Member
521 Points
469 Posts
Convert Sql query into linq
Nov 21, 2012 05:41 PM|LINK
Hello All,
I am having problem while creating equvalent LINQ query for the SQL query.
I have folowing SQL query:
SELECT Cnt.Id, Cnt.Headline, Cnt.Body, Cnt.CreateDateTime, Cnt.ImageUrl, Sb.Name FROM Contents AS Cnt INNER JOIN SubCategories AS Sb ON Cnt.SubcategoryId = Sb.Id WHERE (Sb.CategoryId = 1) AND (Cnt.Id = (SELECT MAX(Id) AS Expr1 FROM Contents WHERE (SubcategoryId = Sb.Id))) GROUP BY Cnt.Id, Cnt.Headline, Cnt.Body, Cnt.CreateDateTime, Cnt.ImageUrl, Sb.Name ORDER BY Cnt.CreateDateTimeNow I want to convert this query into its equvalent LINQ, but I not getting the way how this can be done.
Please tell me how this can be done.
I want to bind the result to a repeater control like below:
var result = LINQ query
repeater1.datasource = result;
repeater1.DataBind();
Please help me, thanks.
RameshRajend...
Star
7983 Points
2099 Posts
Re: Convert Sql query into linq
Nov 21, 2012 05:46 PM|LINK
Try this link:
http://www.sqltolinq.com/[^]
You can download software to Convert Sql to linq or vise versa.
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Convert Sql query into linq
Nov 22, 2012 05:42 AM|LINK
Hello,
Not every SQL can be fully converted to LINQ. So I suggest you trying to manually bind the SQL in the form of a Stored Procdure and bind the result to your manually-created Entity model.
For this issue, you can refer this:http://social.msdn.microsoft.com/Forums/en/adodotnetentityframework/thread/31223561-ad8a-42f2-8c56-e9c756fa120c
Reguards!
sophia_asp
Member
521 Points
469 Posts
Re: Convert Sql query into linq
Nov 24, 2012 05:44 PM|LINK
Hi Ramesh ,
Thanks for your reply, actually I had already found this link but it is paid version. can you please suggest some tutorials which deal
in linq queries like almost what I have asked.
Many thanks
sophia_asp
Member
521 Points
469 Posts
Re: Convert Sql query into linq
Nov 24, 2012 05:53 PM|LINK
Hello Decker,
thanks for link, I think it is good practice to create SPs and then call them using linq as you have suggested.
But link you have provided is very complex for me to understand. Can you please send me a link which
describes about calling stored procedure through linq and then bind result to any of the data bound controls.
Thanks
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Convert Sql query into linq
Nov 25, 2012 01:00 AM|LINK
Hello,
In fact you can first create a Stored Procdure,and then open "View"=>"Server Explorer" in the VS to drag and drop the Stored Procdure into the dbml file.
Reguards!
sophia_asp
Member
521 Points
469 Posts
Re: Convert Sql query into linq
Nov 25, 2012 08:51 AM|LINK
Hello Decker,
Thanks for info but how I call it in code behind file?
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Convert Sql query into linq
Nov 25, 2012 09:50 AM|LINK
Just use it like what you use for LINQ:
using(xxxDataContext dc = new xxxDataContext()) { dc.XXXMethod(); }RameshRajend...
Star
7983 Points
2099 Posts
Re: Convert Sql query into linq
Nov 26, 2012 06:11 AM|LINK
http://stackoverflow.com/questions/1339732/convert-sql-to-linq-to-sql