我正在使用LINQPad将SQL转换为LINQ并在此过程中学习LINQ。我遇到了一些问题,并且希望有人可以查看我生成的LINQ代码,看看我是否在正确的轨道上?
这是我的SQL代码:
Select Count(Convert(varchar(250),
Comment.CommentId)) as Num,
DiscussionBoard.ItemName,
Status.Status,
Status.Sequence
From Comment inner join status on Comment.StatusID = Status.StatusID
inner join DiscussionBoard on Comment.DiscussionBoardID
= DiscussionBoard.DiscussionBoardID
WHERE discussionboard.DiscussionBoardID
= '3ab7c139-317c-4450-9823-45a40ea6d0ff'
Group By status.Status,
Status.Sequence,
DiscussionBoard.ItemName
ORDER BY Status ASC
这是我到目前为止提出的LINQ代码。忍受我,我刚刚学习LINQ,我还没有完全理解它。任何指针都将非常感激:
from Comment in Comments
where DiscussionBoard.DiscussionBoardID == '3ab7c139-317c-4450-9823-45a40ea6d0ff'
join Status in Statuses on Comment.StatusID equals Statuses.StatusID
join DiscussionBoard in DiscussionBoards on Comment.DiscussionBoardID equals DiscucussionBoard.DiscussionBoardID
group CountGroup by new {
Status.Status,
Status.Sequence,
DiscussionBoard.DiscussionBoardID
}
select new
{
Count = CountGroup.Count(),
DiscussionBoard.ItemName,
Status.Status,
Status.Sequence
}
答案 0 :(得分:3)
group x by y
此片段结束查询。
我认为你的意思是:
group x by y into z
此片段在范围内继续使用z进行查询。并从范围中删除所有先前的范围变量。 z是IGrouping<y, x>
,也就是说,密钥类型是y的类型,组元素类型是x&#39; s。
以下是我的查询:
from comment in Comments
where comment.DiscussionBoard.DiscussionBoardID == '3ab7c139-317c-4450-9823-45a40ea6d0ff'
let status = comment.Status
let board = comment.DiscussionBoard
group comment by new {
status.Status,
status.Sequence,
board.ItemName
} into g
select new
{
Count = g.Count(),
ItemName = g.Key.ItemName,
Status = g.Key.Status,
Sequence = g.Key.Sequence
}
打开此查询的另一种方法是:
from board in DiscussionBoards
where board.DiscussionBoardID == '3ab7c139-317c-4450-9823-45a40ea6d0ff'
from comment in board.Comments