我不太了解LINQ。我发现的每个示例都过于简化,以至于我不知道您为什么还要使用 LINQ,或者太复杂以至于我需要知道 LINQ 的工作原理才能理解它。
所以我想知道你是否以及如何使用 LINQ... 每个能力都有等级要求。如何使用 LINQ 为一组特定类选择处于或低于某个级别的所有能力?
鉴于此数据:
- 第一类
- 能力1 - 1级
- 能力 2 - 5 级
- 能力 3 - 9 级
- 2 类
- 能力4 - 1级
- 能力 5 - 2 级
- 能力6 - 6级
- 三级
- 能力 7 - 2 级
- 能力8 - 4级
- 能力 9 - 6 级
我想要 5 级或以下 Class1 和 Class3 的所有能力列表。结果应该是 { Ability1, Ability2, Ability7, Ability8 }。
public class Class1
{
public class Ability
{
public string Name { get; set; }
public int Level { get; set; }
}
public class PlayerClass
{
public string Name { get; set; }
public List<Ability> Abilities { get; set; }
}
public List<PlayerClass> Classes { get; set; }
public Class1()
{
PlayerClass oClass;
Classes = new List<PlayerClass>();
oClass = new PlayerClass();
oClass.Name = "Class1";
oClass.Abilities = new List<Ability>();
oClass.Abilities.Add(new Ability() { Name = "Ability1", Level = 1 });
oClass.Abilities.Add(new Ability() { Name = "Ability2", Level = 5 });
oClass.Abilities.Add(new Ability() { Name = "Ability3", Level = 9 });
Classes.Add(oClass);
oClass = new PlayerClass();
oClass.Name = "Class2";
oClass.Abilities = new List<Ability>();
oClass.Abilities.Add(new Ability() { Name = "Ability4", Level = 1 });
oClass.Abilities.Add(new Ability() { Name = "Ability5", Level = 2 });
oClass.Abilities.Add(new Ability() { Name = "Ability6", Level = 6 });
Classes.Add(oClass);
oClass = new PlayerClass();
oClass.Name = "Class3";
oClass.Abilities = new List<Ability>();
oClass.Abilities.Add(new Ability() { Name = "Ability7", Level = 2 });
oClass.Abilities.Add(new Ability() { Name = "Ability8", Level = 4 });
oClass.Abilities.Add(new Ability() { Name = "Ability9", Level = 6 });
Classes.Add(oClass);
IEnumerable<Ability> Abilities =
GetAbilitiesForClasses("Class1;Class3".Split(';'), 5);
//Abilities should contain Ability1, Ability2, Ability7, Ability8
}
public IEnumerable<Ability> GetAbilitiesForClasses
(string[] asClassNames, int iLevel)
{
// TODO: Use LINQ to return the abilities for the class names
//contained in asClassNames that are at or below level: iLevel
return null;
}
}