0

我确信这是一个简单的,但我不能让它工作。这是我用作参考的 SQL 查询。我想用 LINQ 做同样的事情。

SQL:

select
schedulegroupslist.Schedule_Group_Key,
schedulegroupslist.Group_Name
from schedulegroupslist
inner join schedules on schedules.schedule_group_key = schedulegroupslist.Schedule_Group_Key
where schedules.Active = 1
and schedulegroupslist.Schedule_Count > 0
and schedulegroupslist.Active = 1
group by schedulegroupslist.Schedule_Group_Key
,schedulegroupslist.Group_Name

这是我认为可以解决问题的 linq,但我在组计划行上得到了一个空引用异常。

林克:

var included = from g in allGroups
           join s in allSchedules 
           on g.ScheduleGroupKey equals s.GroupKey into sg
           from schedules in sg.Where(s => s.Active == true 
                                           && g.ScheduleCount > 0 
                                           && g.Active == true).DefaultIfEmpty()
           group schedules by 
                    new { schedules.Key, schedules.ScheduleGroupName } 
                    into schedulesgroup
           select new { GroupName = schedulesgroup.Key.ScheduleGroupName,
                        ScheduleGroupKey = schedulesgroup.Key.Key };

对linq不太流利,所以也许有人可以告诉我一个更好的方法来完成我正在做的事情?

4

2 回答 2

1

尝试这个:

var included = from g in allGroups
               join s in allSchedules on g.ScheduleGroupKey equals s.GroupKey
               where s.Active && g.ScheduleCount > 0 && g.Active
               group s by new { s.Key, s.ScheduleGroupName } into grp
               select new
               {
                   ScheduleGroupKey = grp.Key.Key,
                   GroupName = grp.Key.ScheduleGroupName,
               };

我认为在这种情况下分组是没有用的,所以这可能会更好:

var included = from g in allGroups
               join s in allSchedules on g.ScheduleGroupKey equals s.GroupKey
               where s.Active && g.ScheduleCount > 0 && g.Active
               select new
               {
                   ScheduleGroupKey = s.Key,
                   GroupName = s.ScheduleGroupName,
               };
于 2012-08-23T22:48:45.287 回答
0
var included = from g in allGroups
           join s in allSchedules on g.ScheduleGroupKey equals s.GroupKey
           where s.Active == true 
                 && g.ScheduleCount > 0 
                 && g.Active == true
           group g by new { g.ScheduleGroupKey, g.ScheduleGroupName } into gr
           select new 
           {
               gr.Key.ScheduleGroupKey,
               gr.Key.ScheduleGroupName
           }
于 2012-08-23T22:49:26.700 回答