简体   繁体   中英

Get max value from datetimes (c#)

I have code to get all of values from table column and calculate average salary and max difference between salary dates.

Here is model for Salary

    public int Id { get; set; }
    public DateTime Date { get; set; }
    public decimal Amount { get; set; }
    public Nullable<int> Employee_Id { get; set; }

    public virtual Employee Employee { get; set; }

And here is Viewmodel code

public class SalariesViewModel
{
    public string DepartmentName { get; set; }
    public decimal AverageSalary { get; set; }

    public IEnumerable<DateTime> Dates{ get; set; }
    public double MaxDifference { get; set; }
}

Here is code where I calculate average salary

     var maxdiff = 0;
        List<SalariesViewModel> result = new List<SalariesViewModel>();
        var employees = db.Employees.Select(x => new
        {
            Department = x.Department.Name,
            Name = x.Name,
            Salary = x.Salaries.OrderByDescending(y => y.Date).FirstOrDefault().Amount,
            Date = x.Salaries.OrderByDescending(y => y.Date).FirstOrDefault().Date 

        });



        var data = employees.GroupBy(m => m.Department).Select(x => new SalariesViewModel
        {
            DepartmentName = x.Key,
            AverageSalary = x.Average(y => y.Salary),
            Dates = x.Select(y=> y.Date).ToList()

        }).ToList();



        for (int i = 0; i < data.Count - 1; i++) {


        }

        return data;
    }

Also I need to calculate max difference

I can select only dates and calculate differences between them and find max. And then push to ViewModel.

But I think, it not good experience.

How I can do this in data query or in ViewModel?

Update

This might give some clues to anyway heading down this rabbit hole

In response to Michael Randalls Answer

Yes. But you calculate difference beetween max and min values of date. I need to calculate other. For example employee has 3 salary pays. I need to show max time between those pays. So it would be 3 payment -2 payment , and 2 payment - 1 payment. and if for example 1 variant difference is bigger, I need to show it.

如果您只需要查找相邻元素之间的最大差异,而不需要创建整个differences列表,则可以在跳过初始元素之后将列表与自身一起Zip ,并通过以下方式获取Max

var maxDiff = dates.Zip(dates.Skip(1), (c, n) => (n-c).TotalSeconds).Max();
var maxdiff = 0;
for (int i = 0; i < dates.Count - 1; i++)
{
  var result = (dates[i + 1] - dates[i]).TotalSeconds;
  if (maxdiff < result)
  {
        maxdiff = result;
  }
}

I am a gambling man, am i close?

var data = employees.GroupBy(m => m.Department).Select(x => new SalariesViewModel
{
    DepartmentName = x.Key,
    AverageSalary = x.Average(y => y.Salary),
    MaxDifference = (x.Max(y => y.Date) - x.Min(y => y.Date)).TotalDays,
 }).ToList()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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