none
Windows Form c#, from Linq to regular C# language RRS feed

  • Question

  • Hi all, I got this code and I have a problem "translating/understand" one part of the code that is written on Linq, I'm not very good with Linq and I need this part of code to develop more. Can anyone help me? I know it goes with some loop (for{...}), but I really do not understand. Here's the code for testing and it works.  https://dotnetfiddle.net/lF925a

    var tablePricePerHours = new List<TablePricePerHours>
        {
            new TablePricePerHours{ Price = 10, Tax = 19, Date = new DateTime(2018,1,5)},
            new TablePricePerHours{ Price = 10.5, Tax = 21.5, Date = new DateTime(2018,4,22)},
            new TablePricePerHours{ Price = 11, Tax = 22.5, Date = new DateTime(2018,6,19)},
            new TablePricePerHours{ Price = 11.5, Tax = 23, Date = new DateTime(2018,10,30)},
            new TablePricePerHours{ Price = 12, Tax = 23.2, Date = new DateTime(2018,11,1)}
        };
    
        var tableTotalHours = new List<TableTotalHours>
        {
            new TableTotalHours{ Date = new DateTime(2018,02,15), TotalHours = 5},
            new TableTotalHours{ Date = new DateTime(2018,02,19), TotalHours =10},
            new TableTotalHours{ Date = new DateTime(2018,02,25), TotalHours = 8},
            new TableTotalHours{ Date = new DateTime(2018,03,29), TotalHours = 7.5},
            new TableTotalHours{ Date = new DateTime(2018,07,05), TotalHours = 9},
            new TableTotalHours{ Date = new DateTime(2018,07,06), TotalHours = 1.5},
            new TableTotalHours{ Date = new DateTime(2018,07,07), TotalHours = 12},
            new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
            new TableTotalHours{ Date = new DateTime(2018,10,22), TotalHours = 10},
            new TableTotalHours{ Date = new DateTime(2018,11,1), TotalHours = 8},
            new TableTotalHours{ Date = new DateTime(2018,12,21), TotalHours = 8.5},
            new TableTotalHours{ Date = new DateTime(2018,12,22), TotalHours = 9},
        };
    
    public class TableTotalHours
    {
        public int Id {get;set;}
        public DateTime Date{get;set;}
        public double TotalHours{get;set;}
    }
    
    
    public class TablePricePerHours
    {
        public int Id{get;set;}
        public double Price{get;set;}
        public double Tax{get;set;}
        public DateTime Date{get;set;}
    }

    and now this part is my problem, I need this in the "normal" c# language:

    var result = tableTotalHours.Select(x=> new 
                                        {
                                            Date=x.Date,
                                            TotalHours=x.TotalHours,
                                            CalculatedPrice = tablePricePerHours.Where(c=> (x.Date - c.Date).Ticks>0)
                                                                                .OrderBy(c=> (x.Date - c.Date).Ticks)
                                                                                .First()
                                                                                .Price * x.TotalHours
                                        });

    thanks in advance for help !!!

    Wednesday, January 2, 2019 2:05 AM

Answers

  • static void Main(…)
    {
        ….
        List<Result> r = new List<Result>();
        foreach(var t in tableTotalHours)
        {
            var k = FindNearest(t, tablePricePerHours);
            if (k != null)
                r.Add(k);
        }
    }
    
    static Result FindNearest(TableTotalHours tableTotalHours, List<TablePricePerHours> tablePricePerHours)
            {
                long min = long.MaxValue;
                int index = -1;
                for (int i = 0; i < tablePricePerHours.Count; i++)
                {
                    var ticks = (tableTotalHours.Date - tablePricePerHours[i].Date).Ticks;
                    if (ticks > 0 && ticks < min)
                    {
                        min = ticks;
                        index = i;
                    }
                }
                if (index != -1)
                {
                    return new Result { CalculatedPrice = tablePricePerHours[index].Price * tableTotalHours.TotalHours, Date = tableTotalHours.Date, TotalHours = tableTotalHours.TotalHours };
                }
                return null;
            }
    } //Program class end
    public class Result
        {
            public DateTime Date { get; set; }
            public double TotalHours { get; set; }
            public double CalculatedPrice { get; set; }
        }


    • Marked as answer by techno2018 Thursday, January 3, 2019 2:04 AM
    Wednesday, January 2, 2019 2:45 PM

All replies

  • For each tableTotalHour record you will find nearest record in tablePricePerHour given by Ticks. It is idea. I will write code later.
    Wednesday, January 2, 2019 4:37 AM
  • For each tableTotalHour record you will find nearest record in tablePricePerHour given by Ticks. It is idea. I will write code later.
    Yes, and I think so, but if you could help me with the code, it would help me a lot!
    Wednesday, January 2, 2019 5:56 AM
  • static void Main(…)
    {
        ….
        List<Result> r = new List<Result>();
        foreach(var t in tableTotalHours)
        {
            var k = FindNearest(t, tablePricePerHours);
            if (k != null)
                r.Add(k);
        }
    }
    
    static Result FindNearest(TableTotalHours tableTotalHours, List<TablePricePerHours> tablePricePerHours)
            {
                long min = long.MaxValue;
                int index = -1;
                for (int i = 0; i < tablePricePerHours.Count; i++)
                {
                    var ticks = (tableTotalHours.Date - tablePricePerHours[i].Date).Ticks;
                    if (ticks > 0 && ticks < min)
                    {
                        min = ticks;
                        index = i;
                    }
                }
                if (index != -1)
                {
                    return new Result { CalculatedPrice = tablePricePerHours[index].Price * tableTotalHours.TotalHours, Date = tableTotalHours.Date, TotalHours = tableTotalHours.TotalHours };
                }
                return null;
            }
    } //Program class end
    public class Result
        {
            public DateTime Date { get; set; }
            public double TotalHours { get; set; }
            public double CalculatedPrice { get; set; }
        }


    • Marked as answer by techno2018 Thursday, January 3, 2019 2:04 AM
    Wednesday, January 2, 2019 2:45 PM
  • very good, I am surprised! Thank you very much for your help and support, you saved me a lot of time !!!
    Thursday, January 3, 2019 2:06 AM