[英]How do I use a variable to specify a column name in C#.net (MVC)
I got this table in an SQL database : 我在SQL数据库中获得了此表:
and my code is : 我的代码是:
XXEntities db = new XXEntities();
.
.
public void editSeatsNr(string seatPostion , int BusID)
{
var bus = db.Seats.find(BusID);
}
where Seats is the table name and " seatPostion " Holds the column name ( "A2" for example ) and BusID holds the ID ( 2 for example ) 其中Seats是表名,“ seatPostion ”保留列名(例如“ A2”),BusID保留ID(例如2)
lets say i wanna edit the cell which is the second row and in the "A2" column , but the name of the column "A2" is inside a variable (which is seatPostion ) therefore i cant do this : 可以说我想编辑第二行和“ A2”列中的单元格,但“ A2”列的名称位于变量(为seatPostion )内,因此我不能这样做:
bus.seatPostion = "new value";
any idea ? 任何想法 ?
Here is example how to find out which one to use, Hope this helps 这是如何找出要使用的示例的示例,希望对您有所帮助
static void Main(string[] args)
{
var seatToUse = "A1";
var items = typeof (SeatsList).GetProperties().ToList();
foreach (var item in items)
{
if (item.Name == seatToUse)
{
Console.WriteLine("Match");
}else{
Console.WriteLine("Does not match");
}
}
Console.ReadKey();
}
}
public class SeatsList
{
public int A1 { get; set; }
public int A2 { get; set; }
public int B1 { get; set; }
public int B2 { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.