none
How to get max row per datetoday from icollection ? RRS feed

  • Question

  • Problem

    the following i collection I get all rows 

    ICollection<MachineInfo> lstMachineInfo = manipulator.GetLogData(objZkeeper, int.Parse(tbxMachineNumber.Text.Trim()));

    I need to get lastrow or max row based on field 

    DateTimeRecord

    public class MachineInfo
        {
            public int MachineNumber { get; set; }
            public int IndRegID { get; set; }
            public string DateTimeRecord { get; set; }
    
            public DateTime DateOnlyRecord
            {
                get { return DateTime.Parse(DateTime.Parse(DateTimeRecord).ToString("yyyy-MM-dd")); }
            }
            public DateTime TimeOnlyRecord
            {
                get { return DateTime.Parse(DateTime.Parse(DateTimeRecord).ToString("hh:mm:ss tt")); }
            }
    
        }
    so that how to get last row from machine 

    Wednesday, April 3, 2019 10:34 PM

Answers

  • thank you for reply

    i do as following

     var MachineRows  = lstMachineInfo.Where(x => DateTime.ParseExact(x.DateTimeRecord, "yyyy/MM/dd hh: mm:ss tt", CultureInfo.InvariantCulture) < DateTime.Now).OrderByDescending(m =>Convert.ToDateTime( m.DateTimeRecord)).FirstOrDefault();

    Are this correct please ?

    if correct how to access fields from last row

    FirstOrDefault gives you the first item, in this case if sorting descending then it's the last item while my recommendation works without sorting and if sorting is only done to get the last item I see that as unnecessary, takes more time to process the data then not sorting. 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 4, 2019 12:44 AM
    Moderator

All replies

  • Hello,

    To get the last machine item .Last() when you know there are items in the list. Then .Max for obtaining the max value for a property.

    var machineList = new List<MachineInfo>()
    {
        new MachineInfo()
        {
            MachineNumber = 1, DateTimeRecord = "AAA", IndRegID = 11
        },
        new MachineInfo()
        {
            MachineNumber = 2, DateTimeRecord = "AAA", IndRegID = 22
        }
    };
    
    var results = machineList.Last();
    var maxResult = machineList.Max(machine => machine.MachineNumber);
    

    If unsure there are items in the list

    var machineList = new List<MachineInfo>();
    
    var results = machineList.LastOrDefault();
    if (results == null)
    {
        // don't use results
    }
    else
    {
        // use results
    }


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, April 3, 2019 11:25 PM
    Moderator
  • thank you for reply

    i do as following

     var MachineRows  = lstMachineInfo.Where(x => DateTime.ParseExact(x.DateTimeRecord, "yyyy/MM/dd hh: mm:ss tt", CultureInfo.InvariantCulture) < DateTime.Now).OrderByDescending(m =>Convert.ToDateTime( m.DateTimeRecord)).FirstOrDefault();

    Are this correct please ?

    if correct how to access fields from last row

    Thursday, April 4, 2019 12:26 AM
  • thank you for reply

    i do as following

     var MachineRows  = lstMachineInfo.Where(x => DateTime.ParseExact(x.DateTimeRecord, "yyyy/MM/dd hh: mm:ss tt", CultureInfo.InvariantCulture) < DateTime.Now).OrderByDescending(m =>Convert.ToDateTime( m.DateTimeRecord)).FirstOrDefault();

    Are this correct please ?

    if correct how to access fields from last row

    FirstOrDefault gives you the first item, in this case if sorting descending then it's the last item while my recommendation works without sorting and if sorting is only done to get the last item I see that as unnecessary, takes more time to process the data then not sorting. 

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Thursday, April 4, 2019 12:44 AM
    Moderator