SQL到LINQ转换后的值不正确

时间:2015-04-10 11:33:26

标签: c# mysql linq entity-framework

我需要将以下SQL转换为LINQ

select 
    app.id,
    app.name,
    app.version,
    s.application,
    s.analysis,
    s.behaviour,
    (select count(ia.id)
        from appInstalled ia
        JOIN deviceUser ud ON ia.device_id = ud.device_id
        where ia.app_id = app.id) as deviceCount,
    max(alrt.alert_date)
from application app
left join score s on app.app_md5 = s.md5hash
left join alert alrt on app.id = alrt.app_id 
where app.name like '%gpsnav%'
group by device;

这是我到目前为止所做的事情

    var appQuery = (from app in entities.applications
                          join score in entities.scores on app.app_md5 equals score.md5hash into appScore
                          join alrt in entities.alerts on app.id equals alrt.app_id into appAlerts
                          from s in appScore.DefaultIfEmpty()
                          from a in appAlerts.DefaultIfEmpty()
                          let deviceCount = (from iapp in entities.appInstalled
                                             join ud in entities.deviceUser on iapp.device_id equals ud.device_id
                                             where iapp.id == app.id
                                             select iapp.id).Count()
                          where string.IsNullOrEmpty(searchTerm) || app.name.ToLower().Contains(searchTerm.ToLower())
                          select new
                          {
                              AppId = app.id,
                              AppName = app.name,
                              AppVersion = app.version,
                              AppScore = s.application,
                              Analysis = s.analysis,
                              Behavior = s.behaviour,
                              Devices = deviceCount,
                              AlertDate = a.alert_date
                          });

    var grouped = from a in appQuery
                              group a by new
                              {
                                  a.AppId,
                                  a.AppName,
                                  a.AppVersion,
                                  a.AppScore,
                                  a.Analysis,
                                  a.Behavior,
                                  a.Devices,
                                  a.AlertDate
                              } into g
                              select new
                              {
                                  g.Key.AppId,
                                  g.Key.AppName,
                                  g.Key.AppVersion,
                                  g.Key.AppScore,
                                  g.Key.Analysis,
                                  g.Key.Behavior,
                                  g.Key.Devices,
                                  AlertDate = g.Max(x=>x.AlertDate)
                              };

以上LINQ有效,但Device和AlertDate的数据不正确。我在这里缺少什么?在LINQ中进行分组时,我也没有获得最大的AlertDate。

0 个答案:

没有答案