在列表中将空字符串更改为零

时间:2015-10-14 19:43:30

标签: c# linq

我有一个清单:

library(dplyr)
DF %>% group_by_(.dots=g_vars) %>% summarize_each_(funs(prop_Yes), p_vars)

  grade subject teacher school district    female poverty
  (int)   (chr)   (int)  (int)    (int)     (dbl)   (dbl)
1     4    Math       1      1        1 0.6666667       0
2     4    Math       2      1        1 1.0000000       1
3     4    Math       3      1        1 1.0000000       0
4     4    Math       4      1        1 0.0000000       1
5     4    Math       5      1        1 0.0000000       1

其中一些值是空字符串。我想将所有空字符串更改为“0”。我可以使用1行linq命令吗?

4 个答案:

答案 0 :(得分:6)

如果mono packages/Sake/tools/Sake.exe -I packages/KoreBuild/build -f makefile.shade "$@" 不包含字符串,请不要忘记`Mono JIT compiler version 4.0.4 (Stable 4.0.4.1/5ab4c0d Tue Aug 25 23:11:51 UTC 2015)` `DNU Version: 1.0.0-rc1-15890` `DNX Version: 1.0.0-rc1-15890` 上的ToString()

field

答案 1 :(得分:1)

您可以使用.IsNullOrEmpty()

IEnumerable<string> fields = 
    from field in row.ItemArray
    let s = field.ToString()
    select string.IsNullOrEmpty(s) ? "0" : s;

答案 2 :(得分:-1)

IEnumerable<string> fields = row.ItemArray.Select(field => string.IsNullOrEmpty(field) ? "0" : field.ToString());

答案 3 :(得分:-2)

试试这个:

    private static Action<object, object> CreateSetter(SetterInfo info)
    {
        var propertyInfo = info.Type.GetProperty(info.Name, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance);

        if (propertyInfo == null)
            return (s, v) => { };

        var objParameter = Expression.Parameter(typeof(object));
        var valueParameter = Expression.Parameter(typeof(object));

        //This is the method call I'm trying to add
        if (info.Name[0] == 'G' && info.Type.Name == TaxDataConstant.ParcelFeat)
        {
            var convertParcelFeatCall = Expression.Call(ConvertParcelFeatMethod, valueParameter, Expression.Constant(info.Name));         
        }

        var changeTypeCall = Expression.Call(ChangeTypeMethod, valueParameter, Expression.Constant(propertyInfo.PropertyType));

        var objCast = Expression.Convert(objParameter, info.Type);
        var valueCast = Expression.Convert(changeTypeCall, propertyInfo.PropertyType);

        var property = Expression.Property(objCast, propertyInfo);

        var assignment = Expression.Assign(property, valueCast);

        var lambda = Expression.Lambda<Action<object, object>>(assignment, objParameter, valueParameter);

        return lambda.Compile();
    }