使用LINQ

时间:2019-01-03 07:14:41

标签: 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; }
}

6 个答案:

答案 0 :(得分:8)

您可以使用联接操作,例如

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

Key是:

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

答案 1 :(得分:7)

我不确定这是否是您所需要的:

public static void Main()
{
    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.Join(relationDTOList, 
            dto => dto.PersonId + "-" + dto.RelativeId, 
            dto => dto.RelativeId + "-" + dto.PersonId, 
    (dto1, dto2) => new Relations 
            { 
                PersonId = dto1.PersonId, 
                RelationId = dto1.RelativeId, 
                Relation = dto1.Relation, 
                ReverseRelation = dto2.Relation 
                }).Distinct(new MyEqualityComparer());

    foreach (var g in grp)
        Console.WriteLine("{0},{1},{2},{3}", g.PersonId, g.RelationId, g.Relation, g.ReverseRelation);
}

public class MyEqualityComparer : IEqualityComparer<Relations>
{
    public bool Equals(Relations x, Relations y)
    {
        return x.PersonId + "-" + x.RelationId == y.PersonId + "-" + y.RelationId || 
        x.PersonId + "-" + x.RelationId == y.RelationId + "-" + y.PersonId;
    }

    public int GetHashCode(Relations obj)
    {
        return 0;
    }
}

答案 2 :(得分:6)

我有点怀疑LINQ是这里的最佳选择,因为使用查找循环可能会更有效率。但是,如果您确实需要LINQ,则可以执行以下操作

var relations = from person in relationDTOList
    // Match on the exact pair of IDs
    join relative in relationDTOList on
        new { person.PersonId, person.RelativeId } equals
        new { PersonId = relative.RelativeId, RelativeId = relative.PersonId }

    // Build the new structure
    let relation = new Relations {
        PersonId = person.PersonId,
        Relation = person.Relation,
        RelativeId = relative.PersonId,
        ReverseRelation = relative.Relation
    }

    // Order the pairs to find the duplicates
    let ids = new[] {person.PersonId, relative.PersonId}.OrderBy(x => x).ToArray()
    group relation by new { FirstPersonId = ids[0], SecondPersonId = ids[1] }
    into relationGroups

    // Select only the the first of two duplicates
    select relationGroups.First();

此代码的作用是将集合自身与匹配对PersonIdRelativeId相连,然后过滤掉每对的第二条记录,从而得到一个集合,其中第一个人在列表将被视为该关系中的 parent


编辑:我正在谈论的查找方法:

var result = new List<Relations>();
while (relationDTOList.Any())
{
    var person = relationDTOList.First();
    relationDTOList.RemoveAt(0);

    var relative = relationDTOList.Where(x =>
            x.PersonId == person.RelativeId && x.RelativeId == person.PersonId)
        .Select((x, i) => new {Person = x, Index = i}).FirstOrDefault();

    if (relative != null)
    {
        relationDTOList.RemoveAt(relative.Index);
        result.Add(new Relations {
            PersonId = person.PersonId,
            Relation = person.Relation,
            RelativeId = relative.Person.PersonId,
            ReverseRelation = relative.Person.Relation
        });
    }
}

请注意,它会清空您的原始列表,因此,如果您在代码中进一步需要它,则必须进行复制(list.ToList())。

运行该代码的速度大约是我之前发布的join的方法的六倍。我还想出了以下分组方法,该分组方法比 join 的运行速度快得多,尽管它做的非常相似,但它仍然比 lookup-and-remove 方法慢。

var relations = relationDTOList.GroupBy(person =>
        person.PersonId < person.RelativeId
            ? new {FirstPersonId = person.PersonId, SecondPersonId = person.RelativeId}
            : new {FirstPersonId = person.RelativeId, SecondPersonId = person.PersonId})

    .Select(group => new Relations {
        PersonId = group.First().PersonId,
        Relation = group.First().Relation,
        RelativeId = group.First().RelativeId,
        ReverseRelation = group.Last().Relation
    });

答案 3 :(得分:4)

var query = relationDTOList.OrderBy(x=>x.PersonId).GroupJoin(relationDTOList,
p => p.PersonId,
a => a.RelativeId,
(p, al) =>
new
{
     p.PersonId,
     p.RelativeId,
     p.Relation,
     Parrent = al.Where(x => x.PersonId == p.RelativeId && x.RelativeId == p.PersonId).SingleOrDefault().Relation
 }
 ).ToList();

答案 4 :(得分:3)

您可以将GroupbyTuplePersonId的{​​{1}}进行排序,然后选择第一项作为第一关系,第二项选择为反向关系。

演示:

RelativeId

输出:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Example {

    public static class Program {

        public 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" },
            };

            // Group relations into list of lists
            var groups = relationDTOList
                .GroupBy (r => GetOrderedTuple (r.PersonId, r.RelativeId))
                .Select (grp => grp.ToList ()).ToList ();

            // Output original relations and their reverse relations
            foreach (var group in groups) {
                var relation = group.ElementAt (0);
                var reverseRelation = group.ElementAt (1);
                FormattableString relationOutput = $"PersonId={relation.PersonId} RelativeId={relation.RelativeId} Relation={relation.Relation} ReverseRelation={reverseRelation.Relation}";
                Console.WriteLine (relationOutput);
            }
        }

        private static Tuple<int, int> GetOrderedTuple (int n1, int n2) {
            if (n1 < n2) {
                return Tuple.Create (n1, n2);
            }
            return Tuple.Create (n2, n1);
        }
    }
}

答案 5 :(得分:1)

这可以做到。但是它需要原始列表中的副本。

var result = relationDTOList
                .Where(v => v.PersonId < v.RelativeId)
                .GroupJoin(relationDTOList,
                           p => p.PersonId,
                           a => a.RelativeId,
                           (p, al) =>
                                new{
                                    p.PersonId,
                                    p.RelativeId,
                                    p.Relation,
                                    ReverseRelation = al.Where( x => 
                                              x.PersonId == p.RelativeId &&
                                              x.RelativeId == p.PersonId )
                                                .SingleOrDefault()
                                                .Relation} ).ToList();