0

I have table with more than 20 fields, most of them are integers.

How to select only field which have value bigger than zero?.

sometimes I want to select four field, sometimes ten field depend on field value.

var t = db.Salaries.Where(g => g.الرقم == emp_comp_code &&
g.السنة == year && g.الشهر == month).Distinct().ToList().Select(??field value not equal zero??);

1 Answers1

0

You can use reflection to get fields name from object, after call ToList()

  var list = db.Salaries.Where(g => g.الرقم == emp_comp_code &&
  g.السنة == year && g.الشهر == month).Distinct().ToList();

  var type = list.GetType();
  var ObjType = list[0].GetType();
  var props = objType.GetProperties();
  List<string> propNames = new List<string>();
  for (var i = 0; i < list.Count; i++)
  {
      foreach (var property in props)
      {
        if(prop.PropertyType == typeof(int)){ 
           var value = int.Parse(list[i].GetType().GetProperty(property.Name)
                       .GetValue(list[i], null)).ToString());
           if ((int)value > 0)
           {
              propNames.Add(property.Name);
           }
        }
      }
  }

Then try to call the following function that 'Nicholas Butler' described at this answer:

Func<Data,Data> CreateNewStatement( string fields )
{
    // input parameter "o"
    var xParameter = Expression.Parameter( typeof( Data ), "o" );

    // new statement "new Data()"
    var xNew = Expression.New( typeof( Data ) );

    // create initializers
    var bindings = fields.Split( ',' ).Select( o => o.Trim() )
        .Select( o => {

            // property "Field1"
            var mi = typeof( Data ).GetProperty( o );

            // original value "o.Field1"
            var xOriginal = Expression.Property( xParameter, mi );

            // set value "Field1 = o.Field1"
            return Expression.Bind( mi, xOriginal );
        }
    );

    // initialization "new Data { Field1 = o.Field1, Field2 = o.Field2 }"
    var xInit = Expression.MemberInit( xNew, bindings );

    // expression "o => new Data { Field1 = o.Field1, Field2 = o.Field2 }"
    var lambda = Expression.Lambda<Func<Data,Data>>( xInit, xParameter );

    // compile to Func<Data, Data>
    return lambda.Compile();
}
msd
  • 591
  • 7
  • 23
  • GetValue(obj)?what obj should pass, and getvalue take object and index? how to make this right? – M'hammad M'ssabeh Jun 24 '17 at 05:32
  • Message = "Object does not match target type." exception occur when reach var value = property.GetValue(propsObjType,null).ToString(); – M'hammad M'ssabeh Jun 24 '17 at 05:55
  • @M'hammadM'ssabeh can you set a break point and tell me what is the type of `ObjType`? – msd Jun 24 '17 at 06:03
  • Name=Salary , another thing the function ask pass two param , one objType, second the object index and i put index null var value = property.GetValue(propsObjType,null).ToString(); – M'hammad M'ssabeh Jun 24 '17 at 06:07
  • check my edit `list[i].GetType().GetProperty(property.Name).GetValue(list[i], null)` – msd Jun 24 '17 at 06:14