删除所有重复记录,只保留一个记录。

huangapple go评论86阅读模式
英文:

cut off all duplicate records by field leaving one

问题

我在数据库中有一张表,从中提取数据并在应用程序中显示。

大致而言,该表有两个字段:item_id 和 event field。

事件字段被表示为枚举(表中记录的数字)。
删除所有重复记录,只保留一个记录。

结果发现有一个事件是用户不想看到的。

现在有一个小困境。需要按原样显示所有内容,但不能在同一项上多次显示相同的事件。
有这样一个选项。但不适用,因为它只接受所有分组中的1条记录

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

这是类:

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

    public DateTimeOffset? EventDateTime { get; set; }
    
    public BookingEventActionType ActionCode { get; set; }
}

目前的实际结果看起来像数据库截图。

我想避免在 (int)52 上重复显示 ActionCode。

相信解释清楚了。

英文:

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).
删除所有重复记录,只保留一个记录。

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.

答案1

得分: 2

这是您需要的翻译:

听起来你需要这个:

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

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();

huangapple
  • 本文由 发表于 2023年6月1日 04:20:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/76376997.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定