简体   繁体   中英

How do I use a variable to specify a column name in C#.net (MVC)

I got this table in an SQL database :

在此处输入图片说明

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 )

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 :

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; }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM