2

I have a table named Variables which has (Id ,AdId,Variable1,Variable2,Variable3,Variable4,Variable5,Variable6,Variable7,Variable8 ) , and I have a List<string> a which contains a number of strings (maybe 1,3,6,or 8).

My question is how to insert these strings in the List to the Variables table?

I can do such a thing

    Variables v=new Variables();
    v.AdId=2;

if(a.count()==1){

v.variable1=a[0];
}else if(a.count()==2){
v.variable1=a[0];
v.variable2=a[1]
}else if (//so on){}

But

I want to do something more dynamically like so:

Variables v=new Variables();
    v.AdId=2;
    for(int i=0; i<a.count() ;i++)
    {
         //here list items to be inserted to (variable1,variable2.....variable8)
         //, depending on list size ,number of variables are inserted
    }
rabar kareem
  • 630
  • 9
  • 28

4 Answers4

4

You can use Reflection to access the properties of the Variable model dynamically using the GetProperty method.

        Variables v = new Variables();
        var a = new List<int>{ 1, 2, 3, 4, 5, 6 , 7};
        for (int i = 0; i < a.Count; i++)
        {
            String propertyName = "Variable" + i;
            Type myType = v.GetType();
            try
            {
                myType.GetProperty(propertyName).SetValue(v, a[i].ToString());
            }
            catch (NullReferenceException nre)
            {
                Console.WriteLine("Cannot Find Property");
            }
        }

As you can see I access the property of the object using the string propertyName so you could manipulate that string in such a way that it access the property you want to modify.

I used a try and catch block in the end if the property we are trying to access is not defined in the class / model.

Also take note that Reflection can be a slow solution. You can look for ways to improve it's performance drastically though.

Thanks. Hope that helps.

Community
  • 1
  • 1
Andrei dela Cruz
  • 628
  • 4
  • 11
  • WOOOOW , Worked ! , That was exactly what I was desiring , thank you for your Answer (You genius) – rabar kareem Aug 14 '13 at 11:58
  • But @Andrei why its slow? – rabar kareem Aug 14 '13 at 15:31
  • 1
    I believe it is slow because Reflection checks the metadata and if you use reflection a lot it will be relatively slower than normal property assignments. You could check this link to help you understand the trade-offs - http://stackoverflow.com/questions/771524/how-slow-is-reflection – Andrei dela Cruz Aug 15 '13 at 03:23
2

You can try below as a sample when using Linq to entities. For adding Column values later you can use as an option a factory method called CreateT, where T is the name of the entity class.

Now since your List<string> can have variable number of items, Entity class doesn't has a dynamic way to get Columns like: Variables.Columns[i] OR Variables["ColumnName"]. So using any loop is feasible but still you need to check all the values from a[1] to a[9].

I'm assuming that the List<string> a contains values in sequence. i.e a[1] contains the corresponding value for Column: AdId etc..

 NorthwindEntities db = new NorthwindEntities();
    Variables vrbl = Variables.CreateVariables(a[0]);// usually have atleast one value 
 if(!String.IsNullOrEmpty(a[1]))
    AdId = a[1]; 
 if(!String.IsNullOrEmpty(a[2]))
    Variable1= a[2]; // assign the corresponding values from List of strings 
 if(!String.IsNullOrEmpty(a[3]))
    Variable2= a[3];
 if(!String.IsNullOrEmpty(a[4])) 
    Variable3= a[4];
 if(!String.IsNullOrEmpty(a[5])) 
    Variable4= a[5];
 if(!String.IsNullOrEmpty(a[6]))
    Variable5= a[6];
 if(!String.IsNullOrEmpty(a[7])) 
    Variable6= a[7];
 if(!String.IsNullOrEmpty(a[8])) 
    Variable7= a[8];
 if(!String.IsNullOrEmpty(a[9])) 
    Variable8= a[9];

    db.Variables.AddObject(vrbl); 
    db.SaveChanges();
R.C
  • 10,417
  • 2
  • 35
  • 48
  • yes Its correct but sometimes I may have 2 list items, some times 3, til 8 , how to deal with this case , do I have to take all possibilities? or there is some dynamic thing , like looping throw table colomns! – rabar kareem Aug 14 '13 at 10:31
  • The Dynamic options aren't available much with EF, You can check my updated answer – R.C Aug 14 '13 at 11:02
1

Try this.For fast rply.. may be dirty solution

You can use the switch case statement.

Variables v=new Variables();
v.AdId=2;
for(int i=0; i<a.count() ;i++)
{
switch (a.count())
    { 
        case 0:
            v.Variable1 = a[i].ToString();
            break;
        case 1:
            v.Variable1 = a[i].ToString();
            v.Variable2 = a[i++].ToString();
         case 2:
            v.Variable1 = a[i].ToString();
            v.Variable2 = a[i++].ToString();
            v.Variable2 = a[i+2].ToString();
            break;

and so on.....

    }
}
Umesh Sehta
  • 10,555
  • 5
  • 39
  • 68
1

You can write indexer for you Variables class. But inner implementation have to use either switch or reflection for setting value to property.

Piotr Auguscik
  • 3,651
  • 1
  • 22
  • 30