在LINQ的选择子句中条件 [英] Conditions inside Select clause in Linq

查看:120
本文介绍了在LINQ的选择子句中条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个获取对象的列表,然后一个方法返回一个基于属性的特性和类型的所有属性值。
 我有困难选择的值,因为选择取决于条件。
 我所做的,到目前为止可以让我在我所见过的最丑陋的方式值:

I have a method that gets a list of objects and then returns all their properties values based on the attributes and types of the property.
I'm having difficulties with selecting the value because the Select depends on conditions.
What I did so far allows me to get the value in the ugliest way I have ever seen:

public IEnumerable<string> GetValues(List<ProperyInfo> objects)
{
     var allValues = new List<string>();
     foreach (var obj in objects)
     {
         // Get the PropertyInfo for the obj
         var properties = GetPropertiesWithTheAttributes(obj);
         var values = properties.Select(x => new 
             {
             Value = x.GetValue(obj, null) == null
                         ?  string.empty
                         :
                         x.PropertyType.BaseType == typeof(Enum)
                         ? Convert.ToInt32(x.GetValue(obj, null)).ToString()
                         : (x.GetValue(obj, null)).ToString(),
             ((ReportAttribute)
             Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order
             })
             .OrderBy(x => x.Order)
             .Select(x => x.Value.ToString())
             .ToList();

             allValues.AddRange(values);
    }

    return allValues;
}

而在code我在这里发表我甚至删除了检查的DisplayDate财产在ReportAttribute,如果属性的日期时间属性将被显示为日期也验证或日期时间...

And in the code I published here I even removed the check for the DisplayDate property in the ReportAttribute, which verifies if the datetime property of the attribute would be displayed as date or datetime...

宁静吧!

推荐答案

我想简单地提取这种成2种方法,并且摆脱了三元运算符的:

I would simply extract this into 2 methods, and get rid of the ternary operator:

    // not sure how to name 'obj' and 'x' without more context
    private static object GetValue(PropertyInfo obj, PropertyInfo x)
    {
        if (x.GetValue(obj, null) == null) return string.Empty;

        if (x.PropertyType.BaseType == typeof (Enum))
            return Convert.ToInt32(x.GetValue(obj, null));

        return x.GetValue(obj, null);
    }

    private static int GetOrder(PropertyInfo x)
    {
        return ((ReportAttribute) Attribute.GetCustomAttribute(x, typeof(ReportAttribute), false)).Order;
    }

所以,你可以这样写:

So you can write:

    public IEnumerable<string> GetValues(List<PropertyInfo> objects)
    {
        var allValues = new List<string>();
        foreach (var obj in objects)
        {
            // Get the PropertyInfo for the obj 
            var properties = GetPropertiesWithTheAttributes(obj);
            var values = properties.Select(x => new
                                                    {
                                                        Value = GetValue(obj, x),
                                                        Order = GetOrder(x)
                                                    })
                .OrderBy(x => x.Order)
                .Select(x => x.Value.ToString())
                .ToList();

            allValues.AddRange(values);
        }

        return allValues;
    }

如果你真的想内联这一点,你可以在你的选择更改拉姆达EX pression给一个说法:

If you really want to inline this, you can change the lambda expression in your Select to a statement:

.Select(x =>
            {
                object value;
                if (x.GetValue(obj, null) == null) value = string.Empty;
                else if (x.PropertyType.BaseType == typeof (Enum))
                    value = Convert.ToInt32(x.GetValue(obj, null));
                else value = x.GetValue(obj, null);

                return new
                            {
                                Value = value,
                                ((ReportAttribute)
                                Attribute.GetCustomAttribute(x, typeof (ReportAttribute), false)).
                                    Order
                            };
            })

这篇关于在LINQ的选择子句中条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆