Search code examples
c#linq

cut off all duplicate records by field leaving one


I have a table in the database from which data is pulled out and displayed in the application.

Roughly speaking, the table has two fields: item_id and event field.

The event field is presented as a enum (the number in the table is recorded). enter image description here

It turned out that there is an event that users do not want to see.

Now there is a little dilemma. It is necessary to display everything as it was, but not display the same event over this item more than once. There is such an option. But it is not suitable, because it takes only 1 record of all grouped

var dummyVariable = result
                    .GroupBy(x => x.ActionCode)
                    .Select(group => group.First())
                    .ToList();

This is the class:

public class GetBookingEventsRpcResponse
{
    public long BookingEventId { get; set; }

    public DateTimeOffset? EventDateTime { get; set; }

    public BookingEventActionType ActionCode { get; set; }
}

The actual result now looks like on screenshot of database. I want to avoid dublication of ActionCode for (int)52.

Believe the explanation is clear.


Solution

  • It sounds to me that you need this:

    var dummyVariable =
        result
            .GroupBy(x => x.ActionCode)
            .SelectMany(group => group.Key == 52 ? group.Take(1) : group)
            .ToList();