[英]Help with simple Linq count statement
var q = dc.tblHelpCentreQuestions.Where(c => c.userID == UserID);
q.OrderByDescending(c => c.dateSubmitted);
This works fine, but I also need to return the count of records returned from tblHelpCentreReplies
where QuestionID
equals tblHelpCentreQuestions.ID
. 这工作正常,但我还需要返回从
tblHelpCentreReplies
返回的记录计数,其中QuestionID
等于tblHelpCentreQuestions.ID
。 This is easy enough for me in SQL, can someone show me how this is done in LINQ to SQL? 在SQL中,这对我来说很容易,有人可以告诉我在LINQ to SQL中是如何完成的吗?
I've got as far as this: 我到目前为止:
var q =
from question in dc.tblHelpCentreQuestions
join replies in dc.tblHelpCentreReplies on question.ID
equals replies.ticketID
where question.userID == UserID
orderby question.dateSubmitted descending
select new { question, replies.Count() };
But replies.Count() throws: 但是replies.Count()抛出:
Invalid anonymous type member declarator.
无效的匿名类型成员声明符。 Anonymous type members must be declared with a member assignment, simple name or member access.
必须使用成员分配,简单名称或成员访问来声明匿名类型成员。
the linq query would look like this: linq查询如下所示:
var q =
dc.tblHelpCentreQuestions.
Where(question => question.userID == UserID).
OrderByDescending(question => question.dateSubmitted).
GroupJoin(
dc.tblHelpCentreReplies,
question => question.ID,
replies => replies.ticketID,
(question, replies) => new {Question = question, RepliesCount = replies.Count()});
update if you have a relation mapping than that could be a bit easier 如果您有关系映射,请进行更新 ,否则可能会容易一些
var q =
dc.tblHelpCentreQuestions.
Where(question => question.userID == UserID).
OrderByDescending(question => question.dateSubmitted).
Select(question => new {Question = question, RepliesCount = question.Replies.Count()});
This is easier than you might imagine :-) 这比您想象的要容易:-)
var q =
from question in dc.tblHelpCentreQuestions
where question.userID == UserID
orderby question.dateSubmitted desc
select new { question, question.Replies.Count() };
var q =
from question in dc.tblHelpCentreQuestions
join replies in dc.tblHelpCentreReplies on question.ID equals replies.ticketID
where question.userID == UserID
orderby question.dateSubmitted descending
select new { Question = question, RepliesCount = replies.Count()};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.