【发布时间】:2021-02-11 04:34:18
【问题描述】:
我在 asp.net 核心中有一个 api(顺便说一句,我是 apis 新手),我试图让结果按类属性和该属性值过滤。如果我有一个名为info 的类,它的属性是attack、defense 等...属性的类型是int。用户希望看到attack > 5 或defense > 7 等的结果...这是我的课程:
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
}
这是我的控制器:
[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;
}
我想做的是使用returnValues 作为champions 对象中所有值的列表
Where(x => x.property > value).ToList().
当我使用GetType().GetProperty().GetValue() > value 时,我试图将一个对象与int 进行比较.Where 需要bool 进行过滤。所以我的问题是如何实现这一点?还是有更好的办法?
【问题讨论】:
标签: c# asp.net api linq reflection