user584018
user584018

Reputation: 11364

how to get first item from a group and prepare Data class object

I have list of packets where I am doing ordering and group based on DateAndTime of Result and Name of Packet. and also selecting only First item.

 var packets = new List<Packet>
        {
            new Packet { Name = "P1", Result = new Result { Name = "N1", DateAndTime = new DateTime(2020, 05, 11, 10, 30, 50) }},
            new Packet { Name = "P1", Result = new Result { Name = "N2", DateAndTime = new DateTime(2020, 05, 11, 10, 31, 50) }},
            new Packet { Name = "P2", Result = new Result { Name = "N1", DateAndTime = new DateTime(2020, 05, 11, 10, 32, 50) }},
            new Packet { Name = "P1", Result = new Result { Name = "N2", DateAndTime = new DateTime(2020, 05, 11, 10, 33, 50) }},
            new Packet { Name = "P2", Result = new Result { Name = "N2", DateAndTime = new DateTime(2020, 05, 11, 10, 34, 50) }},
            new Packet { Name = "P1", Result = new Result { Name = "N1", DateAndTime = new DateTime(2020, 05, 11, 10, 35, 50) }}
        };

        var resultData = packets.OrderByDescending(x => x.Result.DateAndTime).GroupBy(x => x.Name).Select(x => x.First());

Supporting Classes,

public class Packet
{
    public string Name { get; set; }
    public Result Result { get; set; }
}

public class Result
{
    public string Name { get; set; }
    public DateTime DateAndTime { get; set; }
}

Now I need to prepare Data object from above resultData,

public class Data
{
    public string PacketName { get; set; }
    public DateTime PacketTime { get; set; }
}


var data=new Data
   {
     PacketName = resultData.???

    }

How to do this? Also OrderByDescending when to apply?

Upvotes: 2

Views: 113

Answers (4)

Hidayet R. Colkusu
Hidayet R. Colkusu

Reputation: 384

List<Data> datas = resultData.Select(x => new Data (){ PacketName = x.Name, PacketTime = x.Result.DateAndTime }).ToList(); 

Or

List<Data> datas = packets.OrderByDescending(x => x.Result.DateAndTime)
                          .GroupBy(x => x.Name) 
                          .Select(x => new Data() { PacketName = x.First().Name, PacketTime = x.First().Result.DateAndTime })
                          .ToList();

Upvotes: 2

Vidya Sangle
Vidya Sangle

Reputation: 39

If you want first item from each group , you will need to iterate through resultData and then populate list of data.

List<Data> dataList = new List<Data>();


            foreach (var item in resultData)
            {
                var data = new Data
                {
                    PacketName = item.Name,
                    PacketTime = item.Result.DateAndTime

                };
            }

OR

If you want only first item from result data :


            var data = new Data
            {
                PacketName = resultData.First().Name,
                PacketTime = resultData.First().Result.DateAndTime
            };

Upvotes: 1

Daniel
Daniel

Reputation: 9849

i need to write this as an answer otherwise i cannot post pictures.

this is what your object currently looks like:

enter image description here

i am not getting what you are asking for? can you clarify?

Upvotes: -1

Jakub Kozera
Jakub Kozera

Reputation: 3493

Try this code:

            var resultData = packets.OrderByDescending(x => x.Result.DateAndTime).GroupBy(x => x.Name).Select(x => new { Name = x.Key, DateAndTime = x.Max(x => x.Result.DateAndTime) }).First();

            var data = new Data
            {
                PacketName = resultData.Name,
                PacketTime = resultData.DateAndTime

            };

Upvotes: 0

Related Questions