Search for particular column and update it in linq EF 4.0

70 Views Asked by At

Basically table contain about 50 columns and I have to search for that particular column and update it.

lets say val == 27 then columnName will be Alarm27 and the "onOff" value I have to set in that Alarm27 column.

But question is how do I get this Alarm27 and just update it.

This is what I have tried so far.

   public void UpdateAlarm(int val, bool onOff)
    {
        string alarmName = "Alarm" + val;
        using (ESEntities context = new ESEntities())
        {
            var alarmid = context.OffShoreAlarms.Where(p => p.StationID == (int)TMStation.LQ).Select(p => p.OSAlarmID).FirstOrDefault();
            var alarmMonitor = context.OfSAlarmMonitors.Where(p => p.OSAlarmID == alarmid).Select(p => p).FirstOrDefault();

            switch (val)
            {
                case 1:
                    alarmMonitor.Alarm1 = onOff;
                    context.saveChanges();
                    break;
                case 2:
                    alarmMonitor.Alarm2 = onOff;
                    context.SaveChanges();
                    break;
                    .
                    .
                    .
                    .
                case 50:
                    alarmMonitor.alarm50 = onOff;
                    context.saveChanges();
                    break;
            }

            //TODO: context.SaveChanges(); do update operation.. 

        }
    }
1

There are 1 best solutions below

0
On

I have not practically tried this but I think this may work for you.

string alarmName = "Alarm" + val;
context.OffShoreAlarms.Property(alarmName).CurrentValue = true; 

You can change current Value of Your entity like this. For more info check this Link