【问题标题】:Using GetValue inside of Where clause在 Where 子句中使用 GetValue
【发布时间】:2021-02-11 04:34:18
【问题描述】:

我在 asp.net 核心中有一个 api(顺便说一句,我是 apis 新手),我试图让结果按类属性和该属性值过滤。如果我有一个名为info 的类,它的属性是attackdefense 等...属性的类型是int。用户希望看到attack > 5defense > 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 =&gt; x.property &gt; value).ToList().

当我使用GetType().GetProperty().GetValue() &gt; value 时,我试图将一个对象与int 进行比较.Where 需要bool 进行过滤。所以我的问题是如何实现这一点?还是有更好的办法?

【问题讨论】:

    标签: c# asp.net api linq reflection


    【解决方案1】:

    您应该将该对象转换为属性的实际类型。在这种情况下,它是int。此外,您不需要其他null 参数。只需GetValue(yourObject) 即可。您调用它并将其转换为您的属性的类型。

    试试这个:

    var returnValues = champions.data.Values
        .Where(x => (int) (x.info.GetType()
            .GetProperty(property)
            .GetValue(x.info)) > value);
    

    【讨论】:

    • 这很好,在 .GetValue(i, null) 中将 i 更改为 x.info 并且它可以工作。我会接受它,但只是解决它。
    • 谢谢,我想我一直都需要投。这是一个非常简单的问题,只是没有思考。
    • @stupidQuestions 是的,每当您将反射与GetValue(object) 一起使用时,您都应该将其转换为您的实际类型。
    【解决方案2】:

    试试看:

    x.info.GetType().GetProperties()
     .Where(y=> y.Name.ToLower() == property.ToLower())
     .FirstOrDefault().GetValue(x.info) > value
    

    https://i.stack.imgur.com/rffk4.png

    【讨论】:

    • 首先,y =&gt; y.Name是什么? Name 是反射类的一部分吗?但无论如何它都不起作用,内部的Where 子句说PropertyInfo does not contain a definition for Where
    • y => y.Name:您的属性名称。 GetValue 从该类中获取一个对象并返回对象。你可以试试吗? int.Parse(x.info.GetType().GetProperties().Where(y=> y.Name.ToLower() == property.ToLower()).FirstOrDefault().GetValue(x.info).ToString( ) )> 价值
    • 我觉得有进步。是的,我也会试试这个。我没有看到那条评论。
    猜你喜欢
    • 2016-03-24
    • 2021-03-14
    • 2011-09-19
    • 1970-01-01
    • 1970-01-01
    • 2021-01-10
    • 2016-10-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多