递归查找相关类的列表 [英] Find a list of related classes Recursively

查看:69
本文介绍了递归查找相关类的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个班级代表一个朋友,另一个班级代表一对最好的朋友和一对最好的朋友,并且想知道与每个最好的朋友间接相关的每个朋友,如下所示:

Supose I have a class representing a friend and another one representing pairs of best friends and a list of pairs of best friends and want to know each friend that is indirectly related to each one's best friend, as follows:

class Friend
{
    string Name;
}

class BestFriends
{
    Friend NameFriend1;
    Friend NameFriend2;
}

List<BestFriends> ListOfBestFriends = new List<BestFriends>();

假设我有如下所示的BestFriends对:

Supose I have the BestFriends pairs as follows:

  • 亚当和布莱恩;
  • 布莱恩和克里斯;
  • 克里斯和丹尼尔;
  • 爱迪和伊恩;
  • 布莱恩和约翰.

我想创建一种方法,该方法返回与一个特定朋友间接相关的所有朋友的列表.例如:如果我要让Brian的所有间接朋友,该方法将返回{Adam,Chris,Daniel,John}.

I want to create a method that returns a list of all friends indirectly related to one specific friend. For example: if I want all indirect friends of Brian, the method would return { Adam, Chris, Daniel, John }.

List<Friend> IndirectFriends (Friend friendToHasItsIndirectFriendsFound, List<BestFriends> bestFriendsPairs)
{
    ...
}

我该怎么办?

推荐答案

考虑使用基于集合的操作通过Linq解决此问题.

Consider using set based operations to solve this with Linq.

给出您建议的数据结构,以解决直接朋友和间接朋友都需要解决的问题.有2个直接朋友案例. 1,其中共同的朋友是NameFriend1,另一个是nameFriend2.这两种情况都在IndirectFriends方法的末尾解决了.

Given the data structure you proposed, to solve the problem you need to solve for both direct friends and indirect friends. There are 2 cases for direct friends. 1, where the common friend is NameFriend1, the other is nameFriend2. These two cases are solved at the end of the method IndirectFriends.

对于间接朋友,情况变得更加复杂,因为您将需要将直接朋友的结果两次连接到同一数据集,一次是直接朋友是第二个列表上的NameFriend1,另一次是当它是NameFriend2时.因此,有4个案件需要解决.

For indirect friends, the case gets more complicated, because you will need to join the results of the direct friends to the same dataset twice, one for where the direct friend is NameFriend1 on the 2nd list, another for when it's NameFriend2. That is why there are 4 cases to resolve.

在IndirectFriends方法的末尾,我从列表中排除了共同的朋友,并且仅返回了不同的结果.

At the end of the method IndirectFriends, I exclude the common friend from the list and return only distinct results.

请注意,此代码仅适用于列表中使用相同对象brian以及进行比较的情况.如果要实例化具有相同值的新变量,并希望linq将它们评估为相等,则需要实现以下IComparable接口链接 如何实现IComparable接口?

Please note, this code only works because the same object brian is being used in the list and also for comparisons. If you are instantiating new variables with the same values and you want them to be evaluated by linq as being equal, you'll need to implement the IComparable interface Link below How to Implement IComparable interface?

[TestMethod]
public void TestMethod1()
{
    List<BestFriends> ListOfBestFriends = new List<BestFriends>();
    var adam = new Friend { Name = "Adam" };
    var brian = new Friend { Name = "Brian" };
    var chris = new Friend { Name = "Chris" };
    var daniel = new Friend { Name = "Daniel" };
    var eddie = new Friend { Name = "Eddie" };
    var ian = new Friend { Name = "Ian" };
    var john = new Friend { Name = "John" };

    ListOfBestFriends.Add(new BestFriends { NameFriend1 = adam, NameFriend2 = brian });
    ListOfBestFriends.Add(new BestFriends { NameFriend1 = brian, NameFriend2 = chris });
    ListOfBestFriends.Add(new BestFriends { NameFriend1 = chris, NameFriend2 = daniel });
    ListOfBestFriends.Add(new BestFriends { NameFriend1 = eddie, NameFriend2 = ian });
    ListOfBestFriends.Add(new BestFriends { NameFriend1 = brian, NameFriend2 = john });

    var result = IndirectFriends(brian, ListOfBestFriends);
}

List<Friend> IndirectFriends(Friend commonFriend, List<BestFriends> bestFriendsPairs)
{
    /* Get inDirect Friends where commonfriend = NameFriend2 */
    /*  First list is joined on Namefriend2 and Namefriend1 */
    var l1 =  (from bfp in bestFriendsPairs
                join bfpR in bestFriendsPairs
                on bfp.NameFriend2 equals bfpR.NameFriend1
                where bfp.NameFriend1 == commonFriend
                select bfpR.NameFriend2).ToList();

    /* Get inDirect Friends where commonfriend= NameFriend2 */
    /*  First list is joined on Namefriend2 and Namefriend2 */
    l1.AddRange(from bfp in bestFriendsPairs
        join bfpR in bestFriendsPairs
            on bfp.NameFriend2 equals bfpR.NameFriend2
        where bfp.NameFriend1 == commonFriend
        select bfpR.NameFriend1);

    /* Get InDirect Friends where commonfriend = NameFriend2 */
    /*  First list is joined on Namefriend1 and Namefriend2 */
    l1.AddRange (from bfp in bestFriendsPairs
              join bfpL in bestFriendsPairs
              on bfp.NameFriend1 equals bfpL.NameFriend2
              where bfp.NameFriend2 == commonFriend
              select bfpL.NameFriend1);

    /* Get InDirect Friends where commonfriend= NameFriend2 */
    /*  First list is joined on Namefriend1 and Namefriend1 */
    l1.AddRange(from bfp in bestFriendsPairs
                join bfpL in bestFriendsPairs
                on bfp.NameFriend1 equals bfpL.NameFriend1
                where bfp.NameFriend2 == commonFriend
                select bfpL.NameFriend2);

    /* Get Direct Friends where commonfriend= NameFriend2 */
    l1.AddRange(from bfp in bestFriendsPairs
                where bfp.NameFriend2 == commonFriend
                select bfp.NameFriend1);

    /* Get Direct Friends where commonfriend= NameFriend1 */
    l1.AddRange(from bfp in bestFriendsPairs
                where bfp.NameFriend1 == commonFriend
                select bfp.NameFriend2);

    /*exclude commonfriend, and get distinct */
    return l1.Where(f=>f!= commonFriend).Distinct().ToList();
}

这篇关于递归查找相关类的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆