locked
Select DataRow with latest date using LINQ RRS feed

  • Question

  • User795332263 posted

    Hi

    I have a datatable where there are 3 columns

    ID | Name | Date

    and i have 4 columns 

    1 | ABC | 20/07/2012

    1 | XYZ | 16/07/2012

    1 | PQR | 21/07/2012

    1 | ABC | 22/07/2012

    Now i want the output as 

    1 | XYZ | 16/07/2012

    1 | PQR | 21/07/2012

    1 | ABC | 22/07/2012

    Notice the ABC record has the latest date, I want the result in a datatable after filtering (prefer LINQ in VB.NET)

    Thanks in advance

    Arun

    Monday, July 23, 2012 12:22 AM

Answers

  • User1288683547 posted

    in C#

            var myLINQ = from dt in dtFirst.AsEnumerable()
                         group dt by dt.Field<string>("Name") into grp
                         select new
                             {
                                 ID = grp.Max(T => T.Field<int>("ID")),
                                 Name = grp.Key, 
                                 Date = grp.Max(T => T.Field<DateTime>("Date"))
                             };

    convert to vb.net, took long time :( and it working good

            Dim myLINQ = From grp In From dt In dtFirst.AsEnumerable() _
                                     Group dt By GRP = dt.Field(Of String)("Name") Into Group _
                                     Select New With { _
                                         Key .ID = Group.Max(Function(T) T.Field(Of Integer)("ID")), _
                                         Key .Name = GRP, _
                                         Key .[Date] = Group.Max(Function(T) T.Field(Of DateTime)("Date")) _
                                                    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2012 3:38 AM

All replies

  • User1288683547 posted

    in C#

            var myLINQ = from dt in dtFirst.AsEnumerable()
                         group dt by dt.Field<string>("Name") into grp
                         select new
                             {
                                 ID = grp.Max(T => T.Field<int>("ID")),
                                 Name = grp.Key, 
                                 Date = grp.Max(T => T.Field<DateTime>("Date"))
                             };

    convert to vb.net, took long time :( and it working good

            Dim myLINQ = From grp In From dt In dtFirst.AsEnumerable() _
                                     Group dt By GRP = dt.Field(Of String)("Name") Into Group _
                                     Select New With { _
                                         Key .ID = Group.Max(Function(T) T.Field(Of Integer)("ID")), _
                                         Key .Name = GRP, _
                                         Key .[Date] = Group.Max(Function(T) T.Field(Of DateTime)("Date")) _
                                                    }



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 23, 2012 3:38 AM
  • User795332263 posted

    Thanks Worked Exactly they way I wanted.. :)

    Thursday, July 26, 2012 3:23 AM