C# Linq中Select子句内的条件

C# Linq中Select子句内的条件,c#,.net,linq,C#,.net,Linq,我有一个方法,该方法获取对象列表,然后根据属性和属性类型返回其所有属性值。 我在选择值时遇到困难,因为选择取决于条件。 到目前为止,我所做的一切让我以我所见过的最丑陋的方式获得了价值: public IEnumerable<string> GetValues(List<ProperyInfo> objects) { var allValues = new List<string>(); foreach (var obj in objects

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

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;
}
public IEnumerable GetValues(列表对象)
{
var allValues=新列表();
foreach(对象中的var obj)
{
//获取obj的属性信息
var properties=具有属性的GetProperties(obj);
变量值=属性。选择(x=>new
{
Value=x.GetValue(对象,空)==null
?string.empty
:
x、 PropertyType.BaseType==typeof(枚举)
?Convert.ToInt32(x.GetValue(obj,null)).ToString()
:(x.GetValue(obj,null)).ToString(),
((报告属性)
GetCustomAttribute(x,typeof(ReportAttribute),false)).Order
})
.OrderBy(x=>x.Order)
.Select(x=>x.Value.ToString())
.ToList();
allValues.AddRange(值);
}
返回所有值;
}
在我在这里发布的代码中,我甚至删除了ReportAttribute中DisplayDate属性的检查,它验证属性的datetime属性是否显示为date或datetime


现在就安静吧

我只需将其提取为两种方法,并去掉三元运算符:

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

+这确实解决了问题!谢谢,但是有没有一种方法可以在if statment中写入条件?添加答案,我将标记为已接受答案。再次感谢。您可以将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
                            };
            })