views:

196

answers:

5

How would I covert this query to inline sql or a stored procedure?

var a = from arow in context.post
where arow.post_id == id && arow.post_isdeleted == false
select new
{
     arow.post_id,
     PostComments = from c in context.comment
                    where c.CommentPostID == arow.post_id
                    select new
                    {
                        c.id,
                        c.title     
                    }
}


List<PostType> pt;
foreach (var s in a)
{
     pt = new PostType();
     pt.PostID = s.post_id;

     //how would I use ADO.NET to put this in a custom class?
     foreach(var ctl in s.PostComments)
     {
         ctl.Title = ctl.title;
         pt.CommentT.Add(ctl);
     }
     ptl.Add(pt);
}

Once the inline query has been executed how would I put the information in a custom class? PostComments is a subquery -- so how would I use ADO.NET to put it in a custom class?

+1  A: 

If you mean that there is a relation between Posts and PostComments tables and there are repeated columns in both tables and one comment could be related to more than one post so you can easily create two commands:

-Select * from Posts where post_Id = id AND IsDeleted = 0;
-Select * from Postcomments where id = cid;

and then execute them using Sql Command Adapters on two data tables. and then:

foreach(DataRow dr in PostsTable.Rows)
{
 //Fill the Post Custom class
 SecondTable.DefaultView.RowFilter = string.Format("PostID = {0}",dr["postID"]);
 foreach(DataRow r in SecondTable.Rows)
 {
  //Fill the Comments Custom class
 } 
}

If this is not your case, so could you try to clarify your database structure?

SubPortal
I am sorry. I have edited the query. Yes, PostComments is related to each article(Post). Similar to how comments or related to answers on SO.
Luke101
If u only get one post and all of its comments so you can create only one command and execute both sql statements with it and use sql data reader instead with the method ReadNextResult() to fill the comments.
SubPortal
A: 

I can't test this but something along the lines of:

SELECT 
    p.post_id
    c.id,
    c.title
FROM 
    post p 
WHERE 
    p.id == 'id' and 
    isdeleted = false
INNER JOIN comment c ON c.commentpostid = p.post_id

I capitalize keywords for readability but for the dbs you're using you might need to change that.

Chris S
+1  A: 

Use SQL Profiler to catch the generated query. Copy to new stored procedure and repair input parameters. Create (save) and use it :)

dario-g
A: 
select post_id, id, title from postcomments pc
where post_id = @id and exists(
    select post_id form post p where p.post_id = pc.post_id and isdeleted = false
)

use a DataReader to get the data & just load it in a list with your custom classes

eglasius
+1  A: 
Ahmad Mageed
This is just genious..Don't know how much to thank you. I wish i could up vote this multiple times.. Thank you for explaining this in such detail..I have downloaded linqpad and it helped tremendously. I wish I could hire you for my project. Thanks again
Luke101
@Luke101 glad it helped and thanks :)
Ahmad Mageed