0

我不太了解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;
  }
}
4

4 回答 4

3
public IEnumerable<Ability> GetAbilitiesForClasses(string[] asClassNames, int iLevel)
{
    return Classes
           .Where(X => asClassNames.Contains(X.Name))
           .SelectMany(X => X.Abilities)
           .Where(X => X.Level <= iLevel)
           .ToList();
}
于 2012-09-07T14:36:53.927 回答
3

假设您的播放器类由一个PlayerClass类表示,其Abilities属性为 type IDictionary<string, int>

List<PlayerClass> classes = ...
var results =
    from c in classes
    where c.Name == "Class1" || c.Name == "Class2"
    from ab in c.Abilities // ab is of type KeyValuePair<string, int>
    where ab.Value <= 5
    select ab.Key;

但最好用和属性定义一个Ability类;该属性将是:NameLevelAbilitiesIList<Ability>

List<PlayerClass> classes = ...
var results =
    from c in classes
    where c.Name == "Class1" || c.Name == "Class2"
    from ab in c.Abilities
    where ab.Level <= 5
    select ab.Name;
于 2012-09-07T14:40:05.263 回答
1

如果你只是存储能力,你可以将它们整合到一个结构中,并有一个你可以执行的类列表LINQ

例如:

public List<AbilityClass> Abilities { get; set; }

林克:

 var level9Abilities = (from a in Abilities
                    where.Level == "Level 9"
                 // where a.Level > 9 (if Level is an int)
                  select a).ToList();


 foreach (var lvl in level9Abilities) {
      Console.WriteLine(lvl);
 }
于 2012-09-07T14:37:36.900 回答
1

你可以使用SelectMany,假设:

 public class Class
{
    public List<Ability> Abilities { get; set; }
}

public class Ability
{
    public string Name { get; set; }
    public int Level { get; set; }
}

使用 Linq:

var classes = new List<Class>() {class1, class3};

var output = classes.SelectMany(c => c.Abilities)
            .Where(a => a.Level <= 5);
于 2012-09-07T14:39:45.383 回答