1. <i id='dO3Lx'><tr id='dO3Lx'><dt id='dO3Lx'><q id='dO3Lx'><span id='dO3Lx'><b id='dO3Lx'><form id='dO3Lx'><ins id='dO3Lx'></ins><ul id='dO3Lx'></ul><sub id='dO3Lx'></sub></form><legend id='dO3Lx'></legend><bdo id='dO3Lx'><pre id='dO3Lx'><center id='dO3Lx'></center></pre></bdo></b><th id='dO3Lx'></th></span></q></dt></tr></i><div id='dO3Lx'><tfoot id='dO3Lx'></tfoot><dl id='dO3Lx'><fieldset id='dO3Lx'></fieldset></dl></div>

    <legend id='dO3Lx'><style id='dO3Lx'><dir id='dO3Lx'><q id='dO3Lx'></q></dir></style></legend>
  2. <tfoot id='dO3Lx'></tfoot>
    • <bdo id='dO3Lx'></bdo><ul id='dO3Lx'></ul>

      <small id='dO3Lx'></small><noframes id='dO3Lx'>

      带有 CASE 语句和 SUM 函数的 LINQ 查询

      时间:2023-06-05
    1. <small id='CHzip'></small><noframes id='CHzip'>

        <tbody id='CHzip'></tbody>
        <bdo id='CHzip'></bdo><ul id='CHzip'></ul>

              <tfoot id='CHzip'></tfoot>

              <i id='CHzip'><tr id='CHzip'><dt id='CHzip'><q id='CHzip'><span id='CHzip'><b id='CHzip'><form id='CHzip'><ins id='CHzip'></ins><ul id='CHzip'></ul><sub id='CHzip'></sub></form><legend id='CHzip'></legend><bdo id='CHzip'><pre id='CHzip'><center id='CHzip'></center></pre></bdo></b><th id='CHzip'></th></span></q></dt></tr></i><div id='CHzip'><tfoot id='CHzip'></tfoot><dl id='CHzip'><fieldset id='CHzip'></fieldset></dl></div>

              1. <legend id='CHzip'><style id='CHzip'><dir id='CHzip'><q id='CHzip'></q></dir></style></legend>

                本文介绍了带有 CASE 语句和 SUM 函数的 LINQ 查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                限时送ChatGPT账号..

                我正在努力寻找如何使用 LINQ 查询或 LAMBDA 返回条件和的示例.我都是独立编写的,但是将 CASE 与 SUM 结合起来很麻烦.我很想作弊"并使用 SQL 视图,但我想我会先问.我非常感谢任何建议.这是我要转换的 SQL.

                I'm struggling to find an example of how to return a conditional sum using a LINQ query or LAMBDA. I've written both independently but combining the CASE with SUM is vexing. I'm tempted to "cheat" and use a SQL view, but thought I'd ask first. I greatly appreciate any suggestions. Here's my SQL that I'm looking to convert.

                SELECT     p.product_name, 
                           SUM(CASE WHEN o.order_dt <= getdate() - 1 THEN o.quantity END) AS volume_1day, 
                           SUM(CASE WHEN o.order_dt <= getdate() - 7 THEN o.quantity END) AS volume_7day, 
                           SUM(CASE WHEN o.order_dt <= getdate() - 30 THEN o.quantity END) AS volume_30day, 
                           SUM(o.quantity) AS volume_all
                FROM       products p left outer join orders o on p.product_id = o.product_id
                GROUP BY   p.product_name
                

                推荐答案

                这是一个使用 Northwinds 数据库的示例.这将为您提供您期望的结果,但 SQL 与您的示例不匹配.

                Here is an example using the Northwinds database. This will get you the results that you are expecting but the SQL won't match your example.

                using (var context = new NorthwindEntities())
                {
                    DateTime volumn1Date = DateTime.Today.AddDays(-1);
                    DateTime volumn7Date = DateTime.Today.AddDays(-7);
                    DateTime volumn30Date = DateTime.Today.AddDays(-30);
                
                    var query = from o in context.Order_Details
                                group o by o.Product.ProductName into g
                                select new
                                {
                                    ProductName = g.Key,
                                    Volume1Day = g.Where(d => d.Order.OrderDate.Value <= volumn1Date)
                                                  // cast to Int32? because if no records are found the result will be a null                                              
                                                  .Sum(d => (Int32?) d.Quantity),
                                    Volume7Day = g.Where(d => d.Order.OrderDate.Value <= volumn7Date)
                                                  .Sum(d => (Int32?) d.Quantity),
                                    Volume30Day = g.Where(d => d.Order.OrderDate.Value <= volumn30Date)
                                                   .Sum(d => (Int32?) d.Quantity)
                                };
                
                
                    var list = query.ToList();
                }
                

                这篇关于带有 CASE 语句和 SUM 函数的 LINQ 查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:对象上的Linq SUM? 下一篇:来自不同格式日期的总小时数.C#

                相关文章

                最新文章

              2. <tfoot id='nbMvK'></tfoot>
              3. <legend id='nbMvK'><style id='nbMvK'><dir id='nbMvK'><q id='nbMvK'></q></dir></style></legend>

                1. <small id='nbMvK'></small><noframes id='nbMvK'>

                    <i id='nbMvK'><tr id='nbMvK'><dt id='nbMvK'><q id='nbMvK'><span id='nbMvK'><b id='nbMvK'><form id='nbMvK'><ins id='nbMvK'></ins><ul id='nbMvK'></ul><sub id='nbMvK'></sub></form><legend id='nbMvK'></legend><bdo id='nbMvK'><pre id='nbMvK'><center id='nbMvK'></center></pre></bdo></b><th id='nbMvK'></th></span></q></dt></tr></i><div id='nbMvK'><tfoot id='nbMvK'></tfoot><dl id='nbMvK'><fieldset id='nbMvK'></fieldset></dl></div>

                      <bdo id='nbMvK'></bdo><ul id='nbMvK'></ul>