简体   繁体   English

如何在列表中查找元素<t>名称相同且时间相同或至少相似的地方?</t>

[英]How to find an element in a List<T> where name is the same and its time is the same or at least similar?

I am currently working on a project which uses two methods, one method returns the most accurate player list on a server with the duration the player was on the server, and the second method utilizes a different method which returns a player list with less accuracy and no time, but with an additional value which I need that other method doesn't have.我目前正在开发一个使用两种方法的项目,一种方法返回服务器上最准确的玩家列表以及玩家在服务器上的持续时间,第二种方法使用另一种方法返回一个精度较低的玩家列表和没有时间,但有一个额外的价值,我需要其他方法没有。 To put it in simple terms:简单来说:

Method 1:方法一:

List<PlayerObjectMethod1> playerListMethod1 = GetPlayersFromServerMethod1(serverIp, serverPort);

The class method 1: class方法一:

public string Name { get; set; }
public float Duration { get; set; }

Method 2:方法二:

List<PlayerObjectMethod2> playersFromMethod2 = new List<PlayerObjectMethod2>();

The class method 1: class方法一:

public string Name { get; set; }
public string SpecialValue { get; set; }
public string CustomDuration { get; set; }

Now as you can see the method 2 doesn't officially return duration, however this method is running every 15 seconds, so in theory, I could attach 15 seconds to each player every time it runs.现在你可以看到方法 2并没有正式返回持续时间,但是这个方法每 15 秒运行一次,所以理论上,我可以在每次运行时为每个玩家附加 15 秒。

More background:更多背景:

The parent method runs on a timer every 15seconds.父方法每 15 秒在计时器上运行一次。 There are 5 servers in total for one server (time in between specific server gets scanned) is around 18 seconds, as such each player on each call can be 18 seconds.一个服务器总共有 5 个服务器(扫描特定服务器之间的时间)大约是 18 秒,因此每次呼叫的每个玩家可以是 18 秒。 I need to get an accurate player for that specific value.我需要为那个特定的价值找到一个准确的球员。 Two comparisons I want to do:我想做的两个比较:

  1. If a players name is not 123, only compare the name to get a specific value.如果玩家姓名不是 123,则仅比较姓名以获得特定值。
if(playerListMethod1[i].Name != "123") {
   var index = playersFromMethod2.FindIndex(x => x==playerListMethod1[i].Name)
   playersFromMethod2[index].IsOnline = True;
   playersFromMethod2[index].Duration = playerListMethod1[i].Duration;
}

And now if it is 123 I need to find it by name and duration.现在,如果它是 123,我需要按名称和持续时间找到它。 However, the issue I have is how to upkeep that second list and add 15 seconds to all the players with name 123. As before I would use a list to store old player list value and just clear it and AddRange of the new one.但是,我遇到的问题是如何维护第二个列表并为所有名称为 123 的玩家添加 15 秒。和以前一样,我会使用一个列表来存储旧的玩家列表值,然后清除它并AddRange新的列表值。

Example:例子:

serverNotfPlayerListOld[server.Name].Clear();
serverNotfPlayerListOld[server.Name].AddRange(playersFromMethod2);

So I basically need an idea on how to do this, would I first fill the method2 with players, then check non 123 players, then check 123 players, and then add 15 seconds to the 123 players and at some point the list would get accurate?所以我基本上需要一个关于如何做到这一点的想法,我会先用球员填充方法2,然后检查非 123 名球员,然后检查 123 名球员,然后在 123 名球员上加上 15 秒,在某些时候,名单会变得准确?

Edit :编辑

As mentioned before there are two different methods (two different sources) one gives name and duration, the other name and player id.如前所述,有两种不同的方法(两种不同的来源),一种给出名称和持续时间,另一种给出名称和玩家 ID。 As such, I need to somehow merge that data together.因此,我需要以某种方式将这些数据合并在一起。 To do that I thought I could add my own duration for the second method because it's being run every 45 seconds.为此,我想我可以为第二种方法添加自己的持续时间,因为它每 45 秒运行一次。 The current new code I have:我目前的新代码:

Example of the addition solution添加溶液示例

class Program
{
    static void Main()
    {

        HashSet<A> a = new HashSet<A>()
        {
            // add random values
            new A { Id = "josh", Value = 60, },
            new A { Id = "tom", Value = 60, },
            new A { Id = "koven", Value = 120, },
            new A { Id = "123", Value = 240, },
        };
        HashSet<A> b = new HashSet<A>()
        {
            // add random values (some with Id's from a)
            new A { Id = "tom", Value = 10, },
            new A { Id = "4123", Value = 10, },
            new A { Id = "koven", Value = 65, },
            new A { Id = "5552", Value = 60, },
            new A { Id = "123", Value = 45, },
        };
        IEnumerable<A> c = IdJoin(a, b);
        int i = 0;
        foreach (A element in c)
        {
            Console.WriteLine($"{element.Id}: {element.Value}");
            i++;
        }
        Console.WriteLine($"Count: {i}");
        Console.WriteLine("Press [enter] to continue...");
        Console.ReadLine();
    }
    public static IEnumerable<A> IdJoin(IEnumerable<A> a, IEnumerable<A> b)
    {
        Dictionary<string, A> dictionary = a.ToDictionary(i => i.Id);
        foreach (A element in b)
        {
            if (dictionary.TryGetValue(element.Id, out A sameId))
            {
                if (element.Id == "123")
                {
                    sameId.Value += element.Value;
                }
                else
                {
                    sameId.Value += 45;
                }
            }
            else {
                dictionary.Add(element.Id, element);
            }
        }
        return dictionary.Values;
    }
}
public class A
{
    public string Id;
    public float Value;
}

Issue with this is that if it reads by only name it will bug out as multiple players can have 123. Which is why I need comparison method which gets by name and duration (of few minutes differences) in those two lists and I need help with that.问题在于,如果它仅按名称读取,它将出错,因为多个玩家可以拥有 123 个。这就是为什么我需要在这两个列表中按名称和持续时间(几分钟差异)获取的比较方法,我需要帮助那。 Another example:另一个例子:

Two 123 players join the game.两名 123 名玩家加入游戏。 One list has values [name:123, duration:240],[name:123, duration:60] the other has [name:123, player:7548, customDuration: 225], [name:123, player:7555, customDuration: 90]一个列表具有值[name:123, duration:240],[name:123, duration:60]另一个具有[name:123, player:7548, customDuration: 225], [name:123, player:7555, customDuration: 90]

I need to get which player is which.我需要知道哪个玩家是哪个。

Presuming Id and Value combination makes unique value:假设 Id 和 Value 组合产生唯一值:

class Program
{
    static List<A> firstList;
    static List<A> secondList;
    static List<A> resultList;

    static void Main(string[] args)
    {
        // Fill firstList, secondList with data <your server methodes>

        resultList = new List<A>();

        foreach (var item in firstList)
        {
            var match = secondList.Find(a => a.Equals(item));
            if (match != null)
            {
                if (item.Id == "123")
                {
                    item.Value += match.Value;
                }
                else
                {
                    item.Value += 45;
                }
            }
            resultList.Add(item);
        }

        resultList.AddRange(secondList.Except(firstList));
    }
}

public class A
{
    public string Id;
    public float Value;

    public override bool Equals(Object obj)
    {
        if ((obj == null) || !GetType().Equals(obj.GetType()))
        {
            return false;
        }
        else
        {
            var a = (A)obj;
            return (Id == a.Id) && (Value == a.Value);
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM