c# - 使用 LINQ 对备用对进行分组

标签 c# linq

我正在尝试对 DTOs 的列表进行分组其中包含备用家庭对,以按照以下格式对它们进行分组,以尽量减少重复。

这是我目前拥有的 DTO 结构,如您所见,这些行也可以根据反向关系组合在一起。

+----------+------------+-----------+
| PersonId | RelativeId | Relation  |
+----------+------------+-----------+
|        1 |          2 | "Son"     |
|        2 |          1 | "Father"  |
|        1 |          3 | "Mother"  |
|        3 |          1 | "Son"     |
|        2 |          3 | "Husband" |
|        3 |          2 | "Wife"    |
+----------+------------+-----------+

像这样:

+----------+------------+-----------+-----------------+
| PersonId | RelativeId | Relation  | ReverseRelation |
+----------+------------+-----------+-----------------+
|        1 |          2 | "Son"     | "Father"        |
|        1 |          3 | "Mother"  | "Son"           |
|        2 |          3 | "Husband" | "Wife"          |
+----------+------------+-----------+-----------------+

我正在尝试的代码:

Program.cs

class Program
{
    static void Main(string[] args)
    {
        List<RelationDTO> relationDTOList = new List<RelationDTO>
        {
            new RelationDTO { PersonId = 1, RelativeId = 2, Relation = "Son" },
            new RelationDTO { PersonId = 2, RelativeId = 1, Relation = "Father" },

            new RelationDTO { PersonId = 1, RelativeId = 3, Relation = "Mother" },
            new RelationDTO { PersonId = 3, RelativeId = 1, Relation = "Son" },

            new RelationDTO { PersonId = 2, RelativeId = 3, Relation = "Husband" },
            new RelationDTO { PersonId = 3, RelativeId = 2, Relation = "Wife" },
        };

        var grp = relationDTOList.GroupBy(x => new { x.PersonId }).ToList();
    }
}

RelationDTO.cs

public class RelationDTO
{
    public int PersonId { get; set; }
    public int RelativeId { get; set; }
    public string Relation { get; set; }
}

Relations.cs

public class Relations
{
    public int PersonId { get; set; }
    public int RelativeId { get; set; }
    public string Relation { get; set; }
    public string ReverseRelation { get; set; }
}

最佳答案

你可以使用像这样的连接操作

var result = relationDTOList
.Where(v => v.PersonId < v.RelativeId)
.Join(
    relationDTOList.Where(v => v.PersonId > v.RelativeId),
    v => new Key{PersonId = v.PersonId, RelativeId = v.RelativeId},
    v => new Key{PersonId = v.RelativeId, RelativeId = v.PersonId},
    (p, q) => new Relations
    {
        PersonId = p.PersonId,
        RelativeId = p.RelativeId,
        Relation = p.Relation,
        ReverseRelation = q.Relation
    }
);

是:

public struct Key
{
    public int PersonId { get; set; }
    public int RelativeId { get; set; }
}

关于c# - 使用 LINQ 对备用对进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54017857/

相关文章:

c# - 如何删除 C# 元组列表中的反向重复项

c# - 如何使用 linq/Entity Framework 绑定(bind) gridview?

c# - 将集合转换为字符串的最佳方法

c# - 如何从字符串中获取最后 2 个单词(从倒数第二个空格开始)?

c# - 解析器错误消息 : Could not create type 'WebService.asmx.cs'

c# - VB.Net 'Overridable' 对成员变量声明无效

c# - 存储站点特定字符串值(标题、支持信息)等的最佳位置是什么

索引器的c#程序

c# - 使用 Linq 还是不使用 Linq - 哪个版本更漂亮?

c# - 在 EF 中不使用 NotMapped 属性执行 LINQ 查询