Brief explanation
It seems that part of your question, which may seem complicated, is how to populate the custom class in the same way as the LINQ to SQL query ("L2S" from here in the output) for the anonymous class.
foreach , :
public class PostType
{
public int PostId { get; set; }
public List<PostComment> PostComments { get; set; }
}
public class PostComment
{
public int CommentId { get; set; }
public string Title { get; set; }
}
LINQ T-SQL:
SELECT P.post_id, C.id, C.title
FROM post As P, comment As C
WHERE
P.post_id = @PostId
AND P.post_isdeleted = 0
AND C.CommentPostID = P.post_id
L2S ( . " " ), , P.post_id, C.id C.title. PostType , ( , , , ). .
, List<PostType>, , PostType, post_id. , PostIds, . .
, ADO.NET SqlDataReader.
int postIdInput = 42;
PostType postType = new PostType()
{
PostId = postIdInput,
PostComments = new List<PostComment>()
};
string sqlStatement = @"SELECT P.post_id As PostId, C.id As CommentId, C.title As Title
FROM post As P, comment As C
WHERE
P.post_id = @PostId
AND P.post_isdeleted = 0 -- 0 is false
AND C.CommentPostID = P.post_id";
string sqlConnectionString = "...";
using (SqlConnection conn = new SqlConnection(sqlConnectionString))
{
conn.Open();
SqlCommand command = new SqlCommand(sqlStatement, conn);
command.Parameters.AddWithValue("@PostId", postIdInput);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
int commentId = Int32.Parse(reader["CommentId"].ToString());
string title = reader["Title"].ToString();
PostComment postComment = new PostComment
{
CommentId = commentId,
Title = title
};
postType.PostComments.Add(postComment);
}
}
. , , !
( " ..." )
, , SQL. , , L2S .
1: LINQ SQL ( "" )
, . LINQ SQL - , SQL LINQ.
T-SQL , DataContext:
: , . SQL , , , . , SQL , , . , .
- 2.
DataContext.GetCommand():
var query = ;
string sqlStatement = context.GetCommand(query).CommandText;
, , Immediate - (Console.WriteLine ..).
DataContext.Log:
context.Log = Console.Out;
, , SQL-, . . , Debug, :
2. SQL ADO.NET
, SQL, ADO.NET. , , .
, , , . , , , , :
SELECT [t0].[post_id], [t1].[id], [t1].[title], (
SELECT COUNT(*)
FROM [comment] AS [t2]
WHERE [t2].[id] = [t0].[post_id]
) As [value]
FROM [post] As [t0]
LEFT OUTER JOIN [comment] As [t1] ON [t1].[CommentPostID] = [t0].[post_id]
WHERE ([t0].[post_id] = @p0) AND ([t0].[post_isdeleted] = 0)
ORDER BY [t0].[post_id], [t1].[id]
SELECT COUNT (*)? L2S , , . , . (.. post_id PostId). , SQL @p0... @pn, . / SqlDataReader, , .
( , , ):
SELECT [P].[post_id] As PostId, [C].[id] As CommentId, [C].[title] As Title
FROM [post] As [P]
LEFT OUTER JOIN [comment] As [C] ON [C].[CommentPostID] = [P].[post_id]
WHERE ([P].[post_id] = @PostId) AND ([P].[post_isdeleted] = 0)
SqlDataReader .
, L2S SelectMany, :
var query = from arow in context.post
from c in context.comment
where arow.post_id == id && arow.post_isdeleted == false
&& c.CommentPostID == arow.post_id
select new
{
arow.post_id,
c.id,
c.title
};
SelectMany L2S SQL, :
SELECT [t0].[post_id], [t1].[id], [t1].[title]
FROM [post] As [t0], [comment] As [t1]
WHERE ([t0].[post_id] = @p0) AND ([t0].[post_isdeleted] = 0)
AND ([t1].[CommentPostID] = [t0].[post_id])
LINQPad
, . LINQPad, , - ! LINQPad L2S, SQL SQL, ( /). , #/VB.NET( LINQ to Objects/XML) SQL- .
LINQPad, , :

, , .
, !:)