带有COUNT和GROUP的LINQ Case语句

时间:2016-11-14 07:42:21

标签: c# mysql asp.net-mvc linq

请帮我翻译一下这个Mysql到Linq:

285: handle_udp_packet: New UDP endpoint: local addr aaa.aaa.aaa.aaa:3478, remote addr bbb.bbb.bbb.bbb:49920
285: handle_turn_command: STUN method 0x1 ignored
285: IPv4. Local relay addr: aaa.aaa.aaa.aaa:49180
285: session 001000000000000001: new, realm=<>, username=<>, lifetime=10
285: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
285: handle_udp_packet: New UDP endpoint: local addr aaa.aaa.aaa.aaa:3478, remote addr bbb.bbb.bbb.bbb:49921
285: handle_turn_command: STUN method 0x1 ignored
285: IPv4. Local relay addr: aaa.aaa.aaa.aaa:49183
285: session 002000000000000001: new, realm=<>, username=<>, lifetime=10
285: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
285: handle_turn_command: STUN method 0x1 ignored
285: IPv4. tcp or tls connected to: bbb.bbb.bbb.bbb:53133
285: IPv4. tcp or tls connected to: bbb.bbb.bbb.bbb:53132
285: IPv4. Local relay addr: aaa.aaa.aaa.aaa:49168
285: session 001000000000000002: new, realm=<>, username=<>, lifetime=10
285: session 001000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
285: IPv4. Local relay addr: aaa.aaa.aaa.aaa:49191
285: session 001000000000000003: new, realm=<>, username=<>, lifetime=10
285: session 001000000000000003: realm <> user <>: incoming packet ALLOCATE processed, success
285: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
285: handle_turn_command: STUN method 0x1 ignored
285: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
285: handle_turn_command: STUN method 0x1 ignored
285: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
285: handle_turn_command: STUN method 0x1 ignored
285: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
286: handle_turn_command: STUN method 0x1 ignored
286: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
286: handle_turn_command: STUN method 0x1 ignored
286: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
287: handle_turn_command: STUN method 0x1 ignored
287: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
287: handle_turn_command: STUN method 0x1 ignored
287: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
288: handle_turn_command: STUN method 0x1 ignored
288: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
288: handle_turn_command: STUN method 0x1 ignored
288: handle_udp_packet: New UDP endpoint: local addr aaa.aaa.aaa.aaa:3478, remote addr ccc.ccc.ccc.ccc:47718
288: handle_turn_command: STUN method 0x1 ignored
288: IPv4. Local relay addr: aaa.aaa.aaa.aaa:49187
288: session 003000000000000001: new, realm=<>, username=<>, lifetime=600
288: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
288: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
288: handle_turn_command: STUN method 0x1 ignored
288: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
288: handle_turn_command: STUN method 0x1 ignored
288: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
289: IPv4. tcp or tls connected to: ccc.ccc.ccc.ccc:35396
289: IPv4. Local relay addr: aaa.aaa.aaa.aaa:49166
289: session 003000000000000002: new, realm=<>, username=<>, lifetime=600
289: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
289: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
289: IPv4. tcp or tls connected to: ccc.ccc.ccc.ccc:35397
289: handle_turn_command: STUN method 0x1 ignored
289: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
289: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
289: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
290: handle_turn_command: STUN method 0x1 ignored
290: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
290: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
291: handle_turn_command: STUN method 0x1 ignored
291: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
291: handle_turn_command: STUN method 0x1 ignored
291: session 001000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
291: handle_turn_command: STUN method 0x1 ignored
292: session 002000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
292: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
293: handle_turn_command: STUN method 0x1 ignored
293: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
293: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
294: handle_turn_command: STUN method 0x1 ignored
294: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
295: session 001000000000000001: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote bbb.bbb.bbb.bbb:49920, reason: allocation timeout
295: session 001000000000000001: delete: realm=<>, username=<>
295: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
295: session 002000000000000001: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote bbb.bbb.bbb.bbb:49921, reason: allocation timeout
295: session 002000000000000001: delete: realm=<>, username=<>
295: session 001000000000000002: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote bbb.bbb.bbb.bbb:53133, reason: allocation timeout
295: session 001000000000000002: delete: realm=<>, username=<>
295: session 001000000000000003: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote bbb.bbb.bbb.bbb:53132, reason: allocation timeout
295: session 001000000000000003: delete: realm=<>, username=<>
296: handle_turn_command: STUN method 0x1 ignored
296: session 003000000000000001: realm <> user <>: incoming packet ALLOCATE processed, success
297: session 003000000000000002: realm <> user <>: incoming packet ALLOCATE processed, success
298: handle_turn_command: STUN method 0x1 ignored
298: handle_turn_command: STUN method 0x1 ignored
298: handle_turn_command: STUN method 0x1 ignored
298: handle_turn_command: STUN method 0x1 ignored
298: session 003000000000000001: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote ccc.ccc.ccc.ccc:47718, reason: allocation timeout
298: session 003000000000000001: delete: realm=<>, username=<>
299: session 003000000000000002: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote ccc.ccc.ccc.ccc:35396, reason: allocation timeout
299: session 003000000000000002: delete: realm=<>, username=<>
299: session 002000000000000002: TCP socket closed remotely ccc.ccc.ccc.ccc:35397
299: session 002000000000000002: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote ccc.ccc.ccc.ccc:35397, reason: TCP connection closed by client (callback)
299: handle_udp_packet: New UDP endpoint: local addr aaa.aaa.aaa.aaa:3478, remote addr ccc.ccc.ccc.ccc:47718
299: handle_turn_command: STUN method 0x1 ignored
301: handle_turn_command: STUN method 0x1 ignored
302: handle_turn_command: STUN method 0x1 ignored
328: handle_turn_command: STUN method 0x1 ignored
329: handle_turn_command: STUN method 0x1 ignored
331: handle_turn_command: STUN method 0x1 ignored
333: handle_turn_command: STUN method 0x1 ignored
359: session 003000000000000003: closed (2nd stage), user <> realm <> origin <>, local aaa.aaa.aaa.aaa:3478, remote ccc.ccc.ccc.ccc:47718, reason: allocation watchdog determined stale session state

由于

2 个答案:

答案 0 :(得分:3)

嗯,您需要查看生成的SQL,但过滤和分组部分很简单 - 它只是计数位特别棘手。你可以在小组的投影中做到这一点:

var start = new DateTime(2016, 1, 1);
var end = new DateTime(2016, 10, 1);
var query = from labCase in db.LabCase
            where labCase.Seq != null &&
                labCase.DateServ >= start &&
                labCase.DateServ <= end
            group labCase by labCase.DistCode into g
            select new
            {
                DistCode = g.Key,
                CountId1 = g.Count(x => x.LabId == 1),
                CountId2 = g.Count(x => x.LabId == 2),
                CountId3 = g.Count(x => x.LabId == 3)
            };

答案 1 :(得分:0)

这是Lambda查询

var query = db.labcase
        .Where(p => p.SEQ != null && p.date_serv >=  new Datetime(2016,01,01) && p.date_serv <= new Datetime(2016,01,10))
        .GroupBy(f => new {distcode}
                , (key , elem) => new {
    key.distcode
    ,count_id1 =  elem.Count(x => x.LabId == 1)
    ,count_id2 =  elem.Count(x => x.LabId == 2)
    ,count_id3 =  elem.Count(x => x.LabId == 3)
})