【问题标题】:I want sum all item prices in all rows in the table in database that have the same item id我想对数据库中表中具有相同项目 ID 的所有行中的所有项目价格求和
【发布时间】:2015-04-19 02:31:58
【问题描述】:

我想对数据库表中具有相同商品 ID 的所有行中的所有商品价格求和。我的意思是结果应该是一个数字,当我从下拉列表中选择特定项目 id 时,它会自动放在文本框中

如何在 linq 中做到这一点?

这是实现我想要的还是应该使用Group by。

告诉我正确答案是什么:

    public double oIlstGetVouchersDetailjh(int nvoucherID)
    {
        vouchers ovouchers = new vouchers();
        double sum = (from Entity in ovouchers.INV_InventoryItems
                                    where Entity.ItemID == nvoucherID
                                    select Entity.ItemPrice).Sum();
        return sum;
    }

【问题讨论】:

  • 这个查询给你什么?你期待它给你什么?看起来应该没问题,但是您对数据的其他结构没有太多详细信息。
  • 我没试过..我是编程的初学者,尤其是linq
  • ovouchers.INV_InventoryItems.Where(x => x.ItemID == nvoucherID).Select(x => x.ItemPrice).Aggregate((x,y) => x + y);

标签: c# linq


【解决方案1】:

我认为您需要在数据库中创建一个包含 ID 和 Total 值的新表。您当前的解决方案仅获得 1 个 ID。使用 Group By 将为所有 id 创建总计。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            vouchers ovouchers = new vouchers();
            ovouchers.INV_InventoryItems = new List<inventory>(){
               new inventory{
                   ItemID = 123, ItemPrice = 1
               },
               new inventory{
                   ItemID = 123, ItemPrice = 2
               },
               new inventory{
                   ItemID = 123, ItemPrice = 3
               },
               new inventory{
                   ItemID = 124, ItemPrice = 1
               },
               new inventory{
                   ItemID = 124, ItemPrice = 1
               },

            };

            var inventoryByID = ovouchers.INV_InventoryItems.AsEnumerable()
                .GroupBy(x => x.ItemID, y => y.ItemPrice)
                .Select(x => new { ID = x.Key, total = x.Sum() });
            string boxID = "123";
            double itemID = inventoryByID.Where(x => x.ID == int.Parse(boxID)).Select(y => y.total).FirstOrDefault();

        }
    }
    public class vouchers
    {
        public List<inventory> INV_InventoryItems { get; set; }
    }
    public class inventory
    {
        public int ItemID { get; set; }
        public double ItemPrice { get; set; }
    }
}
​
​

【讨论】:

  • 您好 Jdweng 我想对数据库中表中所有行中具有相同项目 ID 的所有项目价格求和 .....我的意思是结果应该是一个数字,并在出现时自动将其放在文本框中我从下拉列表中选择特定的项目 ID
  • 我修改了代码以从 Group By 查询中获取价格。每次您需要获取 ID 的总价或创建表格结果时进行查询是一种权衡。
  • 我会在 5 小时后尝试,但这是否达到了我想要的效果??我用cs填充写了它,想在aspx.cs中调用它我怎么能做到这一点并将结果放在文本框............
  • 为什么你把 itemprice 放在 group by .??我认为你修改它的代码看起来像我在顶部写的代码是真的?公共双 oIlstGetVouchersDetailjh(int nvoucherID) { 代金券 = 新代金券 (); double sum = (来自 ovouchers.INV_InventoryItems 中的实体,其中 Entity.ItemID == nvoucherID 选择 Entity.ItemPrice).Sum();返回总和; }
猜你喜欢
  • 2013-11-04
  • 2014-11-04
  • 2016-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-01-09
相关资源
最近更新 更多