简体   繁体   English

将datagridview中的日期列与日期时间的月份进行比较 c# winform

[英]Compare the date column in datagridview with month of datetime c# winform

I want to calculate the asset depreciation by comparing the current date and the date of purchase.我想通过比较当前日期和购买日期来计算资产折旧。 I need help because I am doing this quarterly.我需要帮助,因为我每季度做一次。 Compare the date of every quarter with the date of purchase and than calculate the total depreciation to today's date.将每个季度的日期与购买日期进行比较,然后计算到今天的总折旧。 Any Help?任何帮助?

I'm not an accountant.我不是会计师。 Below get you the arrays from a datatable :下面从数据表中获取数组:

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

namespace ConsoleApplication61
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dt = new DataTable();

            dt.Columns.Add("Name", typeof(string));
            dt.Columns.Add("Purchased Date", typeof(DateTime));
            dt.Columns.Add("Price", typeof(decimal));


            dt.Rows.Add(new object[] { "Item A",DateTime.Parse("11/5/2014"), 1000.00 });
            dt.Rows.Add(new object[] { "Item B",DateTime.Parse("2/20/2015"), 1000.00 });
            dt.Rows.Add(new object[] { "Item C",DateTime.Parse("4/5/2016"), 1000.00 });
            dt.Rows.Add(new object[] { "Item D",DateTime.Parse("8/5/2017"), 1000.00 });
            dt.Rows.Add(new object[] { "Item E",DateTime.Parse("9/3/2018"), 1000.00 });
            dt.Rows.Add(new object[] { "Item F",DateTime.Parse("1/5/2015"), 1000.00 });


            DateTime startDate = dt.AsEnumerable().Min(x => x.Field<DateTime>("Purchased Date"));
            DateTime startQuarter = new DateTime(startDate.Year, (3 * (startDate.Month / 3)) + 1, 1 );

            double depreciationRate = .99; //rate per quarter

            DateTime[] quarters = Enumerable.Range(0, (((DateTime.Now - startQuarter).Days) / 90) + 1).Select(x => startQuarter.AddMonths(3 * x)).ToArray();

            var depreciation = dt.AsEnumerable()
                .Select(x => new
                {
                    item = x.Field<string>("Name"),
                    depreciation = quarters
                        .Select(y => x.Field<DateTime>("Purchased Date") > y ? 0 : x.Field<decimal>("Price") *(decimal) Math.Pow(depreciationRate, ((y - x.Field<DateTime>("Purchased Date")).Days) / 90)).ToList()
                })
                .ToList();


        }
    }
}

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

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