Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq-更新动态列_C#_Linq_Entity Framework - Fatal编程技术网

C# Linq-更新动态列

C# Linq-更新动态列,c#,linq,entity-framework,C#,Linq,Entity Framework,我想更新一个动态列。在employee表中,我有各种列,更新哪一列取决于所选内容,因此它是动态的 在employee表中,我有5列-Id、name、desig、depart、Sal 因此,如果我必须更新设计栏:- string columnName = "desig"; // This comes as a parameter. This is a dynamic Value. This is just an example. var data = ctx.tblEmp.Where(e =&g

我想更新一个动态列。在employee表中,我有各种列,更新哪一列取决于所选内容,因此它是动态的

在employee表中,我有5列-Id、name、desig、depart、Sal

因此,如果我必须更新设计栏:-

string columnName = "desig"; // This comes as a parameter. This is a dynamic Value. This is just an example.

var data = ctx.tblEmp.Where(e => e.Id == model.Id).Select(e => e).SingleOrDefault();

data.desig = 'NewValue';

ctx.tblEmp.Attach(data);
...
ctx.SaveChanges();

由于只有5个常量列,我将使用简单的解决方案:

var data = ctx.tblEmp.SingleOrDefault(e => e.Id == model.Id);
switch (columnName)
{
    case "Id":
       data.id = newValue; //newValue should have a correct type.
       break;
    case "name": 
       data.name = newValue; 
       break;
    case "desig":
       data.desig = newValue; 
       break;
    case "depart":
       data.depart = newValue; 
       break;
    case "Sal":
       data.Sal = newValue; 
       break;
}

你有什么问题吗?除了
'NewValue'周围的单引号之外可能无法编译我不知道你在问什么。我还想建议你可以大大简化对这个的数据访问:
var data=ctx.tblEmp.SingleOrDefault(e=>e.Id==model.Id)
新值的类型是什么?类型可以是
int
string
的确,我的表中有更多的列…但我将使用此策略。。!好的,请注意,如果您有许多列或表,反射可能会有所帮助: