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
    }

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

3 Comments

WOOOOW , Worked ! , That was exactly what I was desiring , thank you for your Answer (You genius)
But @Andrei why its slow?
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 - stackoverflow.com/questions/771524/how-slow-is-reflection
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();

2 Comments

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!
The Dynamic options aren't available much with EF, You can check my updated answer
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.....

    }
}

3 Comments

I think of something more dynamically
Yes you are right , but this time I have take all 8 possibilities , is there any shorter way?? cause I need this technique alot
In this way I have to take all 8 possibilities , is there any shorter and more dynamic way?
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.