通过计数和求和将SQL转换为linQ组

时间:2019-02-08 06:53:46

标签: sql-server linq

我有以下sql请求:

inotify

请帮忙吗?从那以后我一直在为此苦苦挣扎。

USERS表如下:

SELECT        
n.CAR_N_ID AS NotId, 
t.USR_N_ID AS UserId, 
tn.CAR_IMAGE as Icon,
tr.LANG_CH_TAG AS UserLanguage, 
SUBSTRING(tr.LABEL, 0, CHARINDEX(':', tr.LABEL)) AS NotifLabel, 
COUNT(n.CAR_N_ID) AS TotalCount, 
SUM(CASE WHEN ttd_dt_End < CONVERT(date, GETDATE()) THEN 1 ELSE 0 END) AS ExpiredCount

FROM USERS AS t INNER JOIN
CARS AS n ON t.NOTI_N_ID = n.NOTI_N_ID INNER JOIN
NOTIFICATIONS_TRAD AS tr ON n.CAR_N_ID = tr.CAR_N_ID
join NOTIFICATIONS as tn on n.CAR_N_ID = tn.CAR_N_ID

WHERE (t.TTD_DT_STOP IS NULL)
GROUP BY n.CAR_N_ID, t.USR_N_ID, tr.LANG_CH_TAG, tr.LABEL, tn.CAR_IMAGE

CARS表如下:

USR_N_ID    NOTI_N_ID   TTD_DT_STOP
2           2101        NULL
2           2103        2019-02-06 16:28:32.580
2           2110        2019-02-06 16:37:58.523
2           2116        2019-02-07 08:36:54.827
2           2122        2019-02-07 08:40:34.317
2           2125        2019-02-07 08:42:07.087
2           2128        NULL
2           2154        2019-02-08 10:05:41.390

NOTIFICATIONS_TRAD表如下:

NOT_N_ID    NOTI_N_ID
13          2101
13          2103
13          2110
13          2116
13          2122
13          2125
13          2128
13          2154

通知表如下:

NOT_N_ID    LANG_CH_TAG     TNOT_CH_LABEL
13          en-GB           Corporate account requires approval :
13          fr-FR           Corporate account requires approval FR :

结果如下:

NOT_CH_IMAGE
icon-proposal

1 个答案:

答案 0 :(得分:0)

因为没有提供诸如CAR_N_IDLabel之类的列:

var users = new List<Users>()
{
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2101, TTD_DT_STOP = null  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2103, TTD_DT_STOP = ParseExact("2019-02-06 16:28:32,580")  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2110, TTD_DT_STOP = ParseExact("2019-02-06 16:37:58,523")  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2116, TTD_DT_STOP = ParseExact("2019-02-07 08:36:54,827")  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2122, TTD_DT_STOP = ParseExact("2019-02-07 08:40:34,317")  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2125, TTD_DT_STOP = ParseExact("2019-02-07 08:42:07,087")  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2128, TTD_DT_STOP = null  },
    new Users(){USR_N_ID = 2, NOTI_N_ID = 2154, TTD_DT_STOP = ParseExact("2019-02-08 10:05:41,390")  }
 };

 var cars = new List<Cars>()
 {
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2101 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2103 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2110 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2116 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2122 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2125 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2128 },
     new Cars{NOT_N_ID = 13, NOTI_N_ID = 2154 },
  };

  var NOTIFICATIONS_TRAD = new List<NOTIFICATIONS_TRAD>()
  {
      new NOTIFICATIONS_TRAD{ NOT_N_ID = 13, LANG_CH_TAG="en-GB", TNOT_CH_LABEL="Corporate account requires approval :" },
      new NOTIFICATIONS_TRAD{ NOT_N_ID = 13, LANG_CH_TAG="fr-FR", TNOT_CH_LABEL="Corporate account requires approval FR :" }
  };

  var NOTIFICATIONS = new List<NOTIFICATIONS>()
  {
      new NOTIFICATIONS{ NOT_CH_IMAGE = "icon-proposal", NOT_N_ID = 13}
  };

并查询:

  var query = (from t in users
        join c in cars on t.NOTI_N_ID equals c.NOTI_N_ID
        join tr in NOTIFICATIONS_TRAD on c.NOT_N_ID equals tr.NOT_N_ID
        join tn in NOTIFICATIONS on c.NOT_N_ID equals tn.NOT_N_ID
        where t.TTD_DT_STOP == null
        group new { c, t, tr } by new { c.NOT_N_ID, t.USR_N_ID, tr.LANG_CH_TAG } into gr
        select new
        {
             gr.Key.USR_N_ID,
             gr.Key.NOT_N_ID,
             gr.Key.LANG_CH_TAG,                                 
             TotalCount = gr.Count(x => x.c.NOT_N_ID != null),
             ExpiredCount = gr.Sum(g => g.t.TTD_DT_STOP < DateTime.Now ? 1 : 0 )
        }).ToList();

和帮助函数来解析datetime

  static DateTime ParseExact(string dateWithTime)
  {
      return DateTime.ParseExact(dateWithTime, "yyyy-MM-dd HH:mm:ss,fff",
                                   System.Globalization.CultureInfo.InvariantCulture);
   }

此外,请记住,您可以通过C#执行原始sql。例如:

var reqRes = yourContext.Database.SqlQuery<YourDTOClass>("SELECT ... FROM YourTable")    
    .ToList();