Linq中Select子句中的条件

时间:2011-11-24 08:12:59

标签: c# .net linq

我有一个获取对象列表的方法,然后根据属性的属性和类型返回所有​​属性值。
 我在选择值时遇到了困难,因为选择取决于条件  到目前为止我所做的让我能够以我所见过的最丑陋的方式获得价值:

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;
}

在我在这里发布的代码中,我甚至删除了ReportAttribute中DisplayDate属性的检查,该属性验证属性的datetime属性是否显示为date或datetime ...

现在宁静!

1 个答案:

答案 0 :(得分:3)

我只是将其提取为2种方法,并摆脱三元运算符:

    // 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;
    }

所以你可以写:

    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;
    }

如果您真的要内联这个,可以将Select中的lambda表达式更改为语句:

.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
                            };
            })