[英]How do I use a variable to specify a column name in C#.net (MVC)
我在SQL數據庫中獲得了此表:
我的代碼是:
XXEntities db = new XXEntities();
.
.
public void editSeatsNr(string seatPostion , int BusID)
{
var bus = db.Seats.find(BusID);
}
其中Seats是表名,“ seatPostion ”保留列名(例如“ A2”),BusID保留ID(例如2)
可以說我想編輯第二行和“ A2”列中的單元格,但“ A2”列的名稱位於變量(為seatPostion )內,因此我不能這樣做:
bus.seatPostion = "new value";
任何想法 ?
這是如何找出要使用的示例的示例,希望對您有所幫助
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.