关于Linq 计算当年和 3 个月的成本的问题

Hui Liu-MSFT 40,866 信誉分 Microsoft 供应商
2024-02-27T09:32:29.7966667+00:00

你好 我正在尝试获取当年和过去 3 个月的成本,但无法弄清楚。你可以帮我吗?顺便说一句,我正在使用 EF Core 6。 提前致谢。

using System;  
using System.Linq;  
using System.Collections.Generic;  
  
  
public class Program  
{  
 public static void Main()  
 {  
  
 // Order collection  
 IList<Order> orderList = new List<Order>() {   
 new Order() { Id = 1, OrderDateTime = new DateTime(2019, 11, 12, 22, 45, 12, 004), CustomerName = "ABC", Status = "Completed",DoneBy="Cenk"} ,  
 new Order() { Id = 2, OrderDateTime = new DateTime(2019, 11, 12, 22, 45, 12, 004),  CustomerName = "A", Status = "Ccancelled",DoneBy="Cenk" } ,  
 new Order() { Id = 3, OrderDateTime = new DateTime(2022, 07, 12, 22, 45, 12, 004),  CustomerName = "dsds", Status = "Contiues",DoneBy="Cenk" } ,  
 new Order() { Id = 4, OrderDateTime = new DateTime(2022, 08, 12, 22, 45, 12, 004) , CustomerName = "dsds", Status = "Completed",DoneBy="Cenk"} ,  
 new Order() { Id = 5, OrderDateTime = new DateTime(2022, 09, 12, 22, 45, 12, 004) , CustomerName = "jdsj",Status = "Completed",DoneBy="Cenk" }   
 };  
  
 // OrderDetail collection  
 IList<OrderDetail> orderDetailList = new List<OrderDetail>() {   
 new OrderDetail() { Id = 1, ProductCode = "12345", ProductName = "modem1",BuyQuantity=1,SellQuantity=10,CostRatio=2 , UnitCost=5,TotalBuyPrice=10,TotalSellPrice=15,Status = "Completed",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=1} ,  
 new OrderDetail() { Id = 2, ProductCode = "Erf7899", ProductName = "modem2",BuyQuantity=2,SellQuantity=10,CostRatio=2 , UnitCost=5,TotalBuyPrice=10,TotalSellPrice=15,Status = "Contiunes",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=1 } ,  
 new OrderDetail() { Id = 3, ProductCode = "0090owd", ProductName = "modem3",BuyQuantity=10,SellQuantity=15,CostRatio=2 , UnitCost=1,TotalBuyPrice=10,TotalSellPrice=15,Status = "Completed",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=1 } ,  
 new OrderDetail() { Id = 4, ProductCode = "fse929", ProductName = "modem4",BuyQuantity=11,SellQuantity=15,CostRatio=2 , UnitCost=5,TotalBuyPrice=10,TotalSellPrice=15,Status = "Cancelled",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=2} ,  
 new OrderDetail() { Id = 5, ProductCode = "1009ksks", ProductName = "modem5",BuyQuantity=19,SellQuantity=1,CostRatio=2 , UnitCost=1,TotalBuyPrice=10,TotalSellPrice=15,Status = "Completed",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=1 },  
     new OrderDetail() { Id = 6, ProductCode = "556dhdk", ProductName = "modem6",BuyQuantity=9,SellQuantity=13,CostRatio=2 , UnitCost=5,TotalBuyPrice=10,TotalSellPrice=15,Status = "Completed",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=2 } ,  
     new OrderDetail() { Id = 7, ProductCode = "99999", ProductName = "modem7",BuyQuantity=10,SellQuantity=14,CostRatio=2 , UnitCost=15,TotalBuyPrice=10,TotalSellPrice=15,Status = "Completed",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=2 },   
     new OrderDetail() { Id = 8, ProductCode = "00000", ProductName = "modem8",BuyQuantity=1,SellQuantity=15,CostRatio=2 , UnitCost=1,TotalBuyPrice=10,TotalSellPrice=15,Status = "Completed",ShippingNumber="Cenk", TrackingNumber="72827", Description="Test", OrderId=1, VendorId=1 }   
 };  
  
 // vendor collection  
 IList<Vendor> vendorList = new List<Vendor>() {   
 new Vendor() { Id = 1, Name = "Vendor1", Address = "ABC Address", Email = "Email", PhoneNumber="1234353", MainResponsibleName="test", AssistantResponsibleName="test1"} ,  
 new Vendor() { Id = 2, Name = "Vendor1", Address = "ABC Address", Email = "Email", PhoneNumber="1234353", MainResponsibleName="test", AssistantResponsibleName="test1" }   
  
 };  
  
 // LINQ Query Syntax to find out teenager students  
  
  var data = orderList.Select(o => new { o.OrderDateTime.Year, o.OrderDateTime.Month, o.OrderDetails.All(d => d.TotalSellPrice) }).GroupBy(x => new { x.Year, x.Month }, (key, group) => new  
 {  
 yr = key.Year,  
 mnth = key.Month,  
 Price = group.Sum(k => k.TotalSellprice)  
 }).ToList();  
     
  
 }  
}  
  
public class Order  
    {  
        public int Id { get; set; }    
        public DateTime OrderDateTime { get; set; }  
        public string CustomerName { get; set; }  
        public string Status { get; set; }  
        public string DoneBy { get; set; }  
        public List<OrderDetail> OrderDetails { get; set; }  
    }  
public class OrderDetail  
    {  
        public int Id { get; set; }  
        public string ProductCode { get; set; }  
        public string ProductName { get; set; }  
        public int BuyQuantity { get; set; }  
        public int SellQuantity { get; set; }  
        public double CostRatio { get; set; }  
        public double UnitCost { get; set; }  
        public double TotalBuyPrice { get; set; }  
        public double TotalSellPrice { get; set; }  
        public string ShippingNumber { get; set; }  
        public string Status { get; set; }  
        public string TrackingNumber { get; set; }  
        public string Description { get; set; }  
        public string Currency { get; set; }  
        public int OrderId { get; set; }  
        public int VendorId { get; set; }  
        public Order Order { get; set; }  
        public Vendor Vendor { get; set; }  
    }  
public class Vendor  
    {  
        public int Id { get; set; }  
        public string Name { get; set; }  
        public string Address { get; set; }  
        public string Email { get; set; }  
        public string PhoneNumber { get; set; }  
        public string MainResponsibleName { get; set; }  
        public string AssistantResponsibleName { get; set; }  
       public List<OrderDetail> OrderDetails { get; set; }  
  
    }


Note:此问题总结整理于Linq Calculate current year and 3 months Cost

Entity Framework Core
Entity Framework Core
实体框架数据访问技术的轻量型、可扩展、开源、跨平台版本。
41 个问题
0 个注释 无注释
{count} 票

接受的答案
  1. Jiale Xue - MSFT 35,556 信誉分 Microsoft 供应商
    2024-02-27T09:45:24.3366667+00:00

    我制作了一个代码示例来测试您的代码并重现您的问题。

    另外,我在您的代码中发现了一些问题。

    首先,我发现你的linq查询没有实体之间的关系。 例如,我们需要设置条件Order.Id==OrderDetail.OrderId。

    其次,您提供的代码不会检查 OrderDateTime 是否是最近 3 个月。

    第三,您在 orderDetailList 中定义了所有 orderId 为 1,但在数据库插入中定义了不同的 orderId。

    我写了一个代码示例来解决这个问题,你可以参考一下。

    code.txt

    结果:

    231829-image.png


    如果答案是正确的,请点击“接受答案”并点赞。 如果您对此答案还有其他疑问,请点击“评论”。

    注意:如果您想接收相关电子邮件,请按照我们的文档中的步骤启用电子邮件通知 此线程的通知。

    1 个人认为此答案很有帮助。
    0 个注释 无注释

0 个其他答案

排序依据: 非常有帮助