我正在尝试从c#中的对象列表中检索某个字符串(让我们调用该列表用户列表)(该列表中有大约1600个元素,每个对象有大约15个不同的参数)。 所以这是我的代码:
private List<String> getGameList(CommandEventArgs e) {
List<String> allgames = new List<string>();
List<User> userlist = e.Server.Users.ToList();
foreach (User u in userlist)
{
if (u.CurrentGame.ToString() != "")
{
if (u.CurrentGame.Value.Name != null)
{
allgames.Insert(0, u.CurrentGame.Value.Name);
}
}
}
return allgames;
}
但是每当这段代码运行时,返回的列表只有一两个结果,而它应该检索更多。
提供有关代码的更多信息:
我知道为什么我只有1或2个结果但不应该更多吗? 如果需要,我可以绝对提供更多信息。
谢谢
答案 0 :(得分:1)
如果没有看到完整的代码,我的猜测是您在循环中的某个地方有一个早期的return
或break
,您可能应该使用continue
代替继续遍历列表。
您也应该使用string.IsNullOrEmpty(s)
而不是与""
进行比较。
这是我能用你提供的信息给予的所有帮助。
答案 1 :(得分:0)
我做了3个小实验,所有3个案例都以某种方式工作:
using System;
using System.Collections.Generic;
using System.Linq;
namespace p44785433
{
class Program
{
static List<User> userList = UserFactory.GenerateUsers();
static void Main(string[] args)
{
Console.WriteLine("Number Of Users: {0}", userList.Count);
var x = getGameList(userList); // 1600 on output
var y = getGameListUpdated(userList); // 1600< on output depend of randomized empty name
var z = getGameListLinq(userList); // 1600< on output depend of randomized empty name
}
private static List<String> getGameList(List<User> parUserList)
{
List<string> allgames = new List<string>();
List<User> userlist = /*e.Server.Users.ToList();*/ parUserList;
foreach (User u in userlist)
{
if (u.CurrentGame.ToString() != "")
{
if (u.CurrentGame.Value.Name != null)
{
allgames.Insert(0, u.CurrentGame.Value.Name);
}
}
}
return allgames;
}
private static List<String> getGameListUpdated(List<User> parUserList)
{
List<string> allgames = new List<string>();
List<User> userlist = /*e.Server.Users.ToList();*/ parUserList;
foreach (User u in userlist)
{
if (!string.IsNullOrEmpty(u.CurrentGame.ToString()))
{
if (!string.IsNullOrEmpty(u.CurrentGame.Value.Name))
{
allgames.Insert(0, u.CurrentGame.Value.Name);
}
}
}
return allgames;
}
private static List<String> getGameListLinq(List<User> parUserList)
{
List<User> userlist = /*e.Server.Users.ToList();*/ parUserList;
var x = userList.Where(r => !string.IsNullOrEmpty(r.CurrentGame.ToString()) && !string.IsNullOrEmpty(r.CurrentGame.Value.Name)); // remove empty
var y = x.Select(r => r.CurrentGame.Value.Name); // select strings
var z = y.Reverse(); // reverse order instead of insert
return z.ToList();
}
}
internal class User
{
public Game CurrentGame { get; set; }
public string Par1 { get; set; }
public string Par2 { get; set; }
public string Par3 { get; set; }
public string Par4 { get; set; }
public string Par5 { get; set; }
public string Par6 { get; set; }
public string Par7 { get; set; }
public string Par8 { get; set; }
public string Par9 { get; set; }
public string Par10 { get; set; }
public string Par11 { get; set; }
public string Par12 { get; set; }
public string Par13 { get; set; }
public string Par14 { get; set; }
internal static User CreateDummy()
{
return new User()
{
CurrentGame = Game.CreateDummy(),
Par6 = UserFactory.Rnd.NextDouble().ToString(),
Par12 = UserFactory.Rnd.NextDouble().ToString()
};
}
}
internal static class UserFactory
{
static Random _rnd;
internal static Random Rnd
{
get
{
if (_rnd == null) _rnd = new Random();
return _rnd;
}
}
internal static List<User> GenerateUsers()
{
List<User> list = new List<User>();
for (int i = 0; i < (int)1600; i++)
{
list.Add(User.CreateDummy());
}
return list;
}
}
internal class Game
{
public Value Value { get; set; }
public string Par1 { get; set; }
public string Par2 { get; set; }
public string Par3 { get; set; }
public string Par4 { get; set; }
public string Par5 { get; set; }
public string Par6 { get; set; }
public string Par7 { get; set; }
public string Par8 { get; set; }
public string Par9 { get; set; }
public string Par10 { get; set; }
public string Par11 { get; set; }
public string Par12 { get; set; }
public string Par13 { get; set; }
public string Par14 { get; set; }
public override string ToString()
{
return Par6;
}
internal static Game CreateDummy()
{
return new Game()
{
Value = Value.CreateDummy(),
Par6 = UserFactory.Rnd.NextDouble().ToString(),
Par12 = UserFactory.Rnd.NextDouble().ToString()
};
}
}
internal class Value
{
public string Name { get; set; }
public string Par1 { get; set; }
public string Par2 { get; set; }
public string Par3 { get; set; }
public string Par4 { get; set; }
public string Par5 { get; set; }
public string Par6 { get; set; }
public string Par7 { get; set; }
public string Par8 { get; set; }
public string Par9 { get; set; }
public string Par10 { get; set; }
public string Par11 { get; set; }
public string Par12 { get; set; }
public string Par13 { get; set; }
public string Par14 { get; set; }
internal static Value CreateDummy()
{
return new Value()
{
Name = (UserFactory.Rnd.Next(10)!=0 ? string.Format("XXA{0}XAA", UserFactory.Rnd.NextDouble()): string.Empty),
Par5 = UserFactory.Rnd.NextDouble().ToString(),
Par11 = UserFactory.Rnd.NextDouble().ToString()
};
}
}
}