mercredi 28 octobre 2020

Using GetValue inside of Where clause

I have an api in asp.net core (I am new to apis btw), and I am trying to have the results filtered by a class property, and that properties value. If I have a class named info and its properties are attack, defense, etc... the types of the properties are int. The user wants to see results where attack > 5 or defense > 7 etc... Here are my classes:

public class Champion
{
    public string version { get; set; }
    public string id { get; set; }
    public string key { get; set; }
    [JsonProperty("name")]
    public string Name { get; set; }
    public string title { get; set; }
    public string blurb { get; set; }
    public info info { get; set; }
    public string[] tags { get; set; }
    //etc
}
public class Root
{
    public Dictionary<string, Champion> data { get; set; }
}
public class info
{
    public int attack { get; set; }
    public int defense { get; set; }
    public int magic { get; set; }
    public int difficulty { get; set; }
}
public class stats
{
    public double hp { get; set; }
    public double hpperlevel { get; set; }
    public double mp { get; set; }
    public double mpperlevel { get; set; }
    //etc
}

Here is my controller:

[HttpGet("{property}/{value}")]    //property is attack, defense, etc
public ActionResult Get(string property, int value)
{
    var json = //json string;
    var champions = JsonConvert.DeserializeObject<Root>(json); 
    info i = new info { };

    //Here is what I am trying to implement
    var returnValues = champions.data.Values
                          .Where(x => x.info.GetType()
                               .GetProperty(property)
                               .GetValue(i, null) > value);

    //remaining code and return statement;
}

What I would like to do is use returnValues as a list of all of the values in the champions object

Where(x => x.property > value).ToList().

When I use GetType().GetProperty().GetValue() > value I am attempting to compare an object to an int .Where requires a bool to filter. So my question is how I can implement this? Or is there a better way?





Aucun commentaire:

Enregistrer un commentaire