首页 \ 问答 \ 使用group / sum / having对LINQ进行SQL查询(SQL query to LINQ using group / sum / having)

使用group / sum / having对LINQ进行SQL查询(SQL query to LINQ using group / sum / having)

我一直在尝试将以下查询从SQL转换为LINQ,我的查询的主要目的是通过同一个Sender查找两个或更多个事务,其中金额的总和> = 2000

SELECT t.TXN_SENDER_ID,
       s.SEN_FIRSTNAME,
       s.SEN_LASTNAME,
       SUM(t.TXN_AMOUNT)
FROM   TRANSACTIONS t
INNER JOIN SENDERS s ON s.SEN_ID = t.TXN_SENDER_ID
WHERE  t.TXN_DATE BETWEEN Dateadd(YEAR, -3, Getdate()) AND Getdate()
GROUP  BY t.SENDER_ID, s.SENDER_NAME, s.SENDER_LASTNAME
HAVING SUM(t.TXN_AMOUNT) > 2000

这是我尝试过的:

var query = from t in context.TRANSACTIONS
            join s in context.SENDERS on t.TXN_SENDER_ID equals s.SEN_ID
            group new { t, s } by new { t.TXN_SENDER_ID, s.SEN_FIRSTNAME, s.SEN_LASTNAME, t.TXN_AMOUNT } into gr
            where (gr.Sum(x => x.t.TXN_AMOUNT) > 1000)
            select new { gr.Key.TXN_AMOUNT, gr.Key.SEN_FIRSTNAME, gr.Key.SEN_LASTNAME };

提前致谢


I've been trying to translate following query from SQL to LINQ, main purpose of my query is to find two or more transactions by the same Sender in which the amount's sum is >= 2000

SELECT t.TXN_SENDER_ID,
       s.SEN_FIRSTNAME,
       s.SEN_LASTNAME,
       SUM(t.TXN_AMOUNT)
FROM   TRANSACTIONS t
INNER JOIN SENDERS s ON s.SEN_ID = t.TXN_SENDER_ID
WHERE  t.TXN_DATE BETWEEN Dateadd(YEAR, -3, Getdate()) AND Getdate()
GROUP  BY t.SENDER_ID, s.SENDER_NAME, s.SENDER_LASTNAME
HAVING SUM(t.TXN_AMOUNT) > 2000

Here's what I've tried:

var query = from t in context.TRANSACTIONS
            join s in context.SENDERS on t.TXN_SENDER_ID equals s.SEN_ID
            group new { t, s } by new { t.TXN_SENDER_ID, s.SEN_FIRSTNAME, s.SEN_LASTNAME, t.TXN_AMOUNT } into gr
            where (gr.Sum(x => x.t.TXN_AMOUNT) > 1000)
            select new { gr.Key.TXN_AMOUNT, gr.Key.SEN_FIRSTNAME, gr.Key.SEN_LASTNAME };

Thanks in advance


原文:https://stackoverflow.com/questions/29754327
更新时间:2022-08-13 11:08

最满意答案

引用重复的帖子

有几种可能的原因。

另一端故意重置连接,我不会在这里记录。 对于应用程序软件来说,这种情况很少见,而且通常是不正确的,但商业软件并不为人所知。

更常见的是,它是通过写入另一端已正常关闭的连接引起的。 换句话说,应用程序协议错误。

在Windows中,“软件导致连接中止”与“连接重置”不同,是由从您的终端发送的网络问题引起的。 有关于此的Microsoft知识库文章。


Quoting a duplicate post

There are several possible causes.

The other end has deliberately reset the connection, in a way which I will not document here. It is rare, and generally incorrect, for application software to do this, but it is not unknown for commercial software.

More commonly, it is caused by writing to a connection that the other end has already closed normally. In other words an application protocol error.

In Windows, 'software caused connection abort', which is not the same as 'connection reset', is caused by network problems sending from your end. There's a Microsoft knowledge base article about this.

相关问答

更多
  • 类似以下的更多信息可能会有所帮助。 在具有“hbase”命令的virtualbox中运行这些命令 hbase zkCli -server sandbox:2181 连接后,您可以在zkcli提示符下运行: ls / 请粘贴上述命令的输出。 “/ hbase-unsecure”的baseZnode似乎不存在。 一般来说,基本znode是“/ hbase”。 请按照@RamPrasadG的建议,通过java代码交叉检查正在运行的程序的类路径 System.out.println(System.getProp ...
  • 经过一些调试后,我发现我的Linux / CentOS服务器在几分钟后就关闭了网络连接 。 在* / proc / sys / net / ipv4 / tcp_keepalive_time *下增加连接保持活动时间后解决了连接超时问题。 Linux控制台示例输出: 以秒为单位显示保持活动时间 [root@hostname ~]# cat /proc/sys/net/ipv4/tcp_keepalive_time 7200 使用以下命令增加值 [root@hostname ~]# echo "21600 ...
  • 正如您在问题的Update 2中看到的那样,问题不是来自客户端应用程序,因为SoapUI发生了同样的错误。 问题是运行客户端应用程序的机器具有低带宽 ,这对于API通信来说是不够的。 使用简单的速度测试,我发现上传带宽低于服务器应用团队给出的最低要求。 我通过在客户端应用程序运行时使用Windows中的资源监视器监视网络资源并使用在线速度检查来得出结论 要解决此问题,必须在客户端应用程序运行的位置增加机器带宽 。 As you can see from Update 2 in the question, t ...
  • 引用重复的帖子 有几种可能的原因。 另一端故意重置连接,我不会在这里记录。 对于应用程序软件来说,这种情况很少见,而且通常是不正确的,但商业软件并不为人所知。 更常见的是,它是通过写入另一端已正常关闭的连接引起的。 换句话说,应用程序协议错误。 在Windows中,“软件导致连接中止”与“连接重置”不同,是由从您的终端发送的网络问题引起的。 有关于此的Microsoft知识库文章。 Quoting a duplicate post There are several possible causes. The ...
  • 您的客户端已将数据从kbd写入套接字并在服务器尝试读取第31行的消息时关闭套接字。似乎是因为您错误地在客户端而不是reader.toString()中执行了reader.readLine() 。 readLine()将使客户端保持在线状态,直到收到服务器响应(或服务器关闭)。 在客户端,您需要更改为 if (input != null) { out.println(input); out.flush(); } println( ...
  • 问题解决了。 我这样做了: openssl pkcs8 -topk8 -nocrypt -outform der -in clientkey.pem -out clientkey.der 但我没有这样做: openssl x509 -outform der -in clientkey.pem -out clientkey.cer 这两个文件都需要通过Java导入密钥库,而不是keytool。 我只导入了clientkey.der。 原来你必须在密钥库中单独导入客户端密钥和服务器证书; 我不知道将.pem ...
  • 如果可能的话,您可以发送一些命令,告诉您的服务器应用程序关闭连接。 If at all possible, you can send some command that will tell your server application to close the connection.
  • 我弄清楚了这个问题。 原来这是一个网络相关的问题。 从我的本地计算机上安装的服务器发出的请求不会通过代理服务器进行路由。 但是我们公司的防火墙不喜欢跳过代理的请求,所以它正在放弃连接。 一旦我将代理路由规划器添加到http请求,我的请求就会通过。 I was able to figure out the issue. It turned out be a network related issue. The requests made from the server installed on my loca ...
  • 消息“由对等方重置连接”表示服务器已关闭连接。 原因可能是TCP超时,磁盘空间不足,ETC。 尝试使用命令行实用程序使用FTP而不使用Java传输文件。 如果出现同样的问题,那肯定不是Java程序。 确保网络对正在传输的文件大小不敏感。 确保服务器在客户端已经进行了“N”之前的连接或经过一段时间后,即20分钟后,不会阻止来自客户端的连接。 查看您的客户端是否可以使用其他协议建立持久TCP连接:SSH等。 如果其他协议也出现问题,则可能是网络问题。 如果您发现问题是由超时引起的,只有当您的连接空闲时间太长时才 ...
  • 您不应该尝试读取比预期更多的数据。 你的循环应该是: int n; int bytesLeft = fileSize; byte[] buffer = new byte[8192]; while (bytesLeft > 0) { int n = bis.read(buffer, 0, Math.min(buffer.length, bytesLeft)); if (n < 0) { throw new EOFException("Expected " + bytesLeft ...

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)