简体   繁体   English

Linq查询(包括)对联接记录进行计数

[英]Linq query with Include to count joined records

I"m using Linqpad connection to Entity Framework to form my Linq query, it's great for seeing your results without having to run your app.. Anyway, I have an Events table, that is joined to an EventStudent table. The EventStudent table will have 0 to many records for each Event. I want to return an event date and title along with the number of students who attended. The EventStudent table has the student as well as a boolean 'attended' field. 我正在使用与Entity Framework的Linqpad连接来形成我的Linq查询,这非常适合查看结果而无需运行您的应用程序。无论如何,我有一个Event表,该表与EventStudent表相连。EventStudent表将具有每个事件0到许多记录。我想返回一个事件的日期和标题以及参加的学生人数。EventStudent表中有该学生以及一个布尔值“ attended”字段。

With this query, I get all the event data and all the student data for each event - this is good! 通过此查询,我可以获得每个事件的所有事件数据和所有学生数据-很好!

from ev in Events.Include("EventStudents")
where (ev.StartDate >= new DateTime(2012, 7, 1) && ev.StartDate <= new DateTime(2013, 6, 30))
orderby ev.StartDate descending
select ev

What I want is the Event information and the COUNT of students who attended, so I can bind this to a grid. 我想要的是活动信息和参加活动的COUNT个学生,因此我可以将其绑定到网格。 This is what I have tried: 这是我尝试过的:

from ev in Events.Include("EventStudents")
where (ev.StartDate >= new DateTime(2012, 7, 1) && ev.StartDate <= new DateTime(2013, 6, 30))
orderby ev.StartDate descending
select new { ev.EventID, ev.StartDate, ev.Title, ev.EventStudents.Count() }

But I get an error "anonymous type member declarator must be simple name... ". 但是我收到一个错误消息“匿名类型成员声明符必须为简单名称...”。 Ok, but, how can I get my event data and the count of students all in one type to bind to my grid? 好的,但是,如何将我的事件数据和学生人数全部以一种类型绑定到网格? If it matters - the grid has a 'select' column, this is why the event ID is included - it will be hidden in the grid. 如果很重要-网格具有“选择”列,这就是为什么包含事件ID的原因-它将隐藏在网格中。 Plus, the grid will have sorting and filtering abilities. 另外,网格将具有排序和过滤功能。 And... how can I return the date from the Linq query with date only (no time?). 而且...如何从Linq查询返回仅具有日期的日期(没有时间?)。

Thank you! 谢谢!

Try this. 尝试这个。 This gets the date from StartDate by using the .Date property. 这是通过使用.Date属性从StartDate获取日期的。 And it names the result of the Count() call as "StudentCount". 并将Count()调用的结果命名为“ StudentCount”。

select new { ev.EventID, ev.StartDate.Date, ev.Title, StudentCount = ev.EventStudents.Count() }

You're query is fine. 您的查询很好。 The only problem is that you need to provide a name for the Count member. 唯一的问题是您需要为Count成员提供一个名称。 Like this: 像这样:

from ev in Events.Include("EventStudents")
where (ev.StartDate >= new DateTime(2012, 7, 1) && ev.StartDate <= new DateTime(2013, 6, 30))
orderby ev.StartDate descending
select new 
{ 
    ev.EventID, 
    ev.StartDate, 
    ev.Title, 
    Count = ev.EventStudents.Count() 
}

From MSDN : MSDN

If you do not specify member names in the anonymous type, the compiler gives the anonymous type members the same name as the property being used to initialize them. 如果未在匿名类型中指定成员名称,则编译器将为匿名类型成员提供与用于初始化它们的属性相同的名称。 You must provide a name for a property that is being initialized with an expression… 您必须为使用表达式初始化的属性提供名称...

You also mentioned you wanted to get the date part only. 您还提到您只想获取日期部分。 You can use the TruncateTime function, like this: 您可以使用TruncateTime函数,如下所示:

from ev in Events.Include("EventStudents")
where (ev.StartDate >= new DateTime(2012, 7, 1) && ev.StartDate <= new DateTime(2013, 6, 30))
orderby ev.StartDate descending
select new 
{ 
    ev.EventID, 
    StartDate = EntityFunctions.TruncateTime(ev.StartDate.Date), 
    ev.Title, 
    Count = ev.EventStudents.Count() 
}

Note you may have to cast the input of this function to a Nullable<DateTime> first, like this: 请注意,您可能必须首先将此函数的输入转换为Nullable<DateTime> ,如下所示:

    StartDate = EntityFunctions.TruncateTime((DateTime?)ev.StartDate.Date), 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM