简体   繁体   English

通过在多个条件下进行联接并将T-SQL查询转换为C#LINQ,并在多个条件下进行分组

[英]Transforming T-SQL Query into C# LINQ with joins on multiple conditions and also grouping on multiple conditions

First I want to say hello, I'm new to this site ;-) 首先,我想打个招呼,我是这个网站的新手;-)

My problem is to transform the following sql-query into a c# linq-query . 我的问题是将以下sql-query转换为c# linq-query

( I HAVE searched hard for an existing answer but I'm not able to combine the solution for the joins on multiple conditions and the grouping / counting ! ) (我一直在努力寻找一个现有的答案,但是我无法结合多种条件下的连接和分组/计算的解决方案!)

The sql-query : sql查询:

DECLARE @datestart AS DATETIME
DECLARE @dateend AS DATETIME

SET @datestart = '01.04.2014'
SET @dateend = '30.04.2014'

SELECT md1.value AS [controller],md2.value AS [action], COUNT(md2.value) AS   [accesscount], MAX(re.TIMESTAMP) AS [lastaccess] FROM recorderentries AS re
INNER JOIN messagedataentries AS md1 ON re.ID = md1.recorderentry_id AND md1.position =  0
INNER JOIN messagedataentries AS md2 ON re.ID = md2.recorderentry_id AND md2.position = 1
WHERE re.TIMESTAMP >= @datestart AND re.TIMESTAMP <= @dateend 
AND re.messageid IN ('ID-01','ID-02' )
GROUP BY md1.value,md2.value
ORDER BY [accesscount] DESC 

Any suggestions are welcome ... 欢迎任何建议...

What i have so far is this : 我到目前为止所拥有的是:

var _RecorderActionCalls = (from r in _DBContext.RecorderEntries
                                    join m1 in _DBContext.MessageDataEntries on
                                     new {
                                        a = r.ID,
                                        b = 0
                                     } equals new {
                                        a = m1.ID,
                                        b = m1.Position
                                     }
                                    join m2 in _DBContext.MessageDataEntries on
                                                       new {
                                                          a = r.ID,
                                                          b = 0
                                                       } equals new {
                                                          a = m2.ID,
                                                          b = m2.Position
                                                       }
                                    where r.TimeStamp >= StartDate & r.TimeStamp <=     EndDate & (r.MessageID == "VAREC_100_01" | r.MessageID == "VAAUTH-100.01")
                                    group r by new { md1 = m1.Value, md2 = m2.Value }    into r1
                                    select new { controller = r1.Key.md1, action =    r1.Key.md2, count = r1.Key.md2.Count() }).ToList();

But this throws an exception ( translated from german ) : 但这引发了一个异常(从德语翻译):

DbExpressionBinding requires an input expression with a Listing Result Type ... DbExpressionBinding需要具有列表结果类型的输入表达式...

UPDATE : Back with headache ... ;-) 更新:头疼... ;-)

I found a solution to my problem : 我找到了解决问题的方法:

var _RecorderActionCalls = _DBContext.RecorderEntries
                                 .Where(r => r.TimeStamp >= StartDate & r.TimeStamp <=     EndDate & (r.MessageID == "VAREC_100_01" | r.MessageID == "VAAUTH-100.01"))
                                 .GroupBy(g => new { key1 =    g.MessageData.FirstOrDefault(md1 => md1.Position == 0).Value, key2 = g.MessageData.FirstOrDefault(md2 => md2.Position == 1).Value })
                                 .Select(s => new {
                                    ControllerAction = s.Key.key1 + " - " + s.Key.key2,
                                    Value = s.Count(),
                                    Last = s.Max(d => d.TimeStamp)
                                 }).ToList();

With this syntax it works for me. 使用此语法,它对我有用。 Thank you for thinking for me :-) 谢谢你为我的想法:-)

Something like that: 像这样:

List<string> messageIdList = new List<string> { "ID-01", "ID-02" };

from re in RecorderEntries
from md1 in MessageDataEntries
from md2 in MessageDataEntries
where re.ID = md1.recorderEntry_id && md1.position == 0
where re.ID = md2.recorderEntry_id && md2.position == 1
where idList.Contains(re.messageid)
let joined = new { re, md1, md2 }
group joined by new { controller = joined.md1.value, action = joined.md2.value } into     grouped
select new { 
    controller = grouped.Key.controller, 
    action = grouped.Key.action,     
    accesscount = grouped.Where(x => x.md2.value != null).Count(), 
    lastaccess = grouped.Max(x => x.re.TimeStamp) }

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

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