質問

I have a context that I want to save all the data in each of the dataset in the context, and I don't want to write code based on each class type. So I want to have list of type that I want to save them and I want to use loop in context.dataset and then loop on the properties.

Could you please help me :

     public static void saveAllFiles(Context context)
      {
            var objectTypes = new List<Type>();
        objectTypes.Add(typeof(Language));
        objectTypes.Add(typeof(Employee));

    foreach(Type objectType in objectTypes)
        {
            //var properties = objectType.GetProperties();
            var properties = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            var dataSetObjects = context.Set<objectType>().ToList();

            foreach(var dataSetObject in dataSetObjects)
            {

                 foreach( var  property in properties)
                 {
                     var value = property.GetValue(dataSetObject);
                     var name= property.Name;
                 }

            }
        }
}
役に立ちましたか?

解決

I'm not sure what class your Context is referring to, but here's some code to give you the idea of how to get the property name and values:

public static void saveAllFiles(Context context)
{
    var objectTypes = new List<Type> {typeof (int), typeof (string)}; 

    foreach(Type objectType in objectTypes)
    {
        var properties = objectType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
        var dataSetObjects = context.Set(objectType);

        foreach(var dataSetObject in dataSetObjects)
        {
            foreach( var property in properties )
            {
                var value = property.GetValue(dataSetObject);
                var name = property.Name;
            }                   
        }
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top