如何在 List<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?
我目前正在开发一个使用两种方法的项目,一种方法 return 是服务器上最准确的玩家列表以及玩家在服务器上的持续时间,第二种方法使用不同的方法return 的玩家列表的方法准确性较低且没有时间,但具有我需要的其他方法没有的附加值。简单来说:
方法一:
List<PlayerObjectMethod1> playerListMethod1 = GetPlayersFromServerMethod1(serverIp, serverPort);
class方法一:
public string Name { get; set; }
public float Duration { get; set; }
方法二:
List<PlayerObjectMethod2> playersFromMethod2 = new List<PlayerObjectMethod2>();
class方法一:
public string Name { get; set; }
public string SpecialValue { get; set; }
public string CustomDuration { get; set; }
现在您可以看到 方法 2 没有正式 return 持续时间,但是此方法每 15 秒 运行ning 一次,所以在理论上,每次 运行s.
我可以为每个玩家附加 15 秒
更多背景:
父方法 运行 每 15 秒在计时器上计时一次。一个服务器总共有 5 个服务器(扫描特定服务器之间的时间)大约为 18 秒,因此每次呼叫的每个玩家可以是 18 秒。我需要为该特定值找到一个准确的播放器。我想做的两个比较:
- 如果一个球员的名字不是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;
}
现在如果是 123,我需要按名称和持续时间找到它。但是,我遇到的问题是如何维护第二个列表并为所有名称为 123 的玩家添加 15 秒。和以前一样,我会使用列表来存储旧玩家列表值并清除它和 AddRange
新的。
示例:
serverNotfPlayerListOld[server.Name].Clear();
serverNotfPlayerListOld[server.Name].AddRange(playersFromMethod2);
所以我基本上需要一个关于如何做到这一点的想法,我会先用玩家填充 method2,然后检查非 123 玩家,然后检查 123 玩家,然后向 123 玩家添加 15 秒,然后在某个时候该列表会变得准确吗?
编辑:
如前所述,有两种不同的方法(两个不同的来源),一种给出名称和持续时间,另一种给出名称和播放器 ID。因此,我需要以某种方式将这些数据合并在一起。为此,我认为我可以为第二种方法添加我自己的持续时间,因为它每 45 秒 运行。我目前的新密码:
加法解例
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;
}
问题在于,如果它仅按名称读取,则会出错,因为多个玩家可以拥有 123 个。这就是为什么我需要比较方法,该方法在这两个列表中按名称和持续时间(相差几分钟)获取我需要帮助。另一个例子:
两位123玩家加入游戏。一个列表的值为 [name:123, duration:240],[name:123, duration:60]
,另一个列表的值为 [name:123, player:7548, customDuration: 225], [name:123, player:7555, customDuration: 90]
我需要知道哪个玩家是哪个。
假定 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);
}
}
}
我目前正在开发一个使用两种方法的项目,一种方法 return 是服务器上最准确的玩家列表以及玩家在服务器上的持续时间,第二种方法使用不同的方法return 的玩家列表的方法准确性较低且没有时间,但具有我需要的其他方法没有的附加值。简单来说:
方法一:
List<PlayerObjectMethod1> playerListMethod1 = GetPlayersFromServerMethod1(serverIp, serverPort);
class方法一:
public string Name { get; set; }
public float Duration { get; set; }
方法二:
List<PlayerObjectMethod2> playersFromMethod2 = new List<PlayerObjectMethod2>();
class方法一:
public string Name { get; set; }
public string SpecialValue { get; set; }
public string CustomDuration { get; set; }
现在您可以看到 方法 2 没有正式 return 持续时间,但是此方法每 15 秒 运行ning 一次,所以在理论上,每次 运行s.
我可以为每个玩家附加 15 秒更多背景:
父方法 运行 每 15 秒在计时器上计时一次。一个服务器总共有 5 个服务器(扫描特定服务器之间的时间)大约为 18 秒,因此每次呼叫的每个玩家可以是 18 秒。我需要为该特定值找到一个准确的播放器。我想做的两个比较:
- 如果一个球员的名字不是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;
}
现在如果是 123,我需要按名称和持续时间找到它。但是,我遇到的问题是如何维护第二个列表并为所有名称为 123 的玩家添加 15 秒。和以前一样,我会使用列表来存储旧玩家列表值并清除它和 AddRange
新的。
示例:
serverNotfPlayerListOld[server.Name].Clear();
serverNotfPlayerListOld[server.Name].AddRange(playersFromMethod2);
所以我基本上需要一个关于如何做到这一点的想法,我会先用玩家填充 method2,然后检查非 123 玩家,然后检查 123 玩家,然后向 123 玩家添加 15 秒,然后在某个时候该列表会变得准确吗?
编辑:
如前所述,有两种不同的方法(两个不同的来源),一种给出名称和持续时间,另一种给出名称和播放器 ID。因此,我需要以某种方式将这些数据合并在一起。为此,我认为我可以为第二种方法添加我自己的持续时间,因为它每 45 秒 运行。我目前的新密码:
加法解例
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;
}
问题在于,如果它仅按名称读取,则会出错,因为多个玩家可以拥有 123 个。这就是为什么我需要比较方法,该方法在这两个列表中按名称和持续时间(相差几分钟)获取我需要帮助。另一个例子:
两位123玩家加入游戏。一个列表的值为 [name:123, duration:240],[name:123, duration:60]
,另一个列表的值为 [name:123, player:7548, customDuration: 225], [name:123, player:7555, customDuration: 90]
我需要知道哪个玩家是哪个。
假定 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);
}
}
}