0

I selected 4 columns from my table by using select statement LINQ to SQL. Then I create a new List to store the query result. BUT, the code room.ToList(); is WRONG.

  1. Why cannot we use "rooms_list = room.ToList()"?

  2. How should we do to solve the problem???

Thanks for teaching me!

private void btnTest_Click(object sender, EventArgs e)
        {
            List<string> rooms_list = new List<string>();

            using (LVDatabaseEntities database = new LVDatabaseEntities())
            {
                var room = from x in database.PHONGCHOIs
                            select new { x.TENPHONG, x.MATKHAUPHONG, x.CHUPHONG, x.SOLUONG };
                rooms_list = room.ToList();
            }
            MessageBox.Show(rooms_list.Count.ToString());
        }
4

1 に答える 1

0

あなたのrooms_list=room.ToList()は、タイプ文字列のリストではないため、間違っています。

これを修正するには、最初に部屋のクラスを作成します

class Room
{
  string TENPHONG { get; set; }
  string MATKHAUPHONG { get; set; }
  string CHUPHONG{ get; set; }
  string SOLUONG{ get; set; }
}

次に、このようにコードを変更します

List<Room> rooms_list = new List<Room>();

using (LVDatabaseEntities database = new LVDatabaseEntities())
{
   var rooms = from x in database.PHONGCHOIs
                    select new Room{ 
    TENPHONG = x.TENPHONG,
    MATKHAUPHONG = x.MATKHAUPHONG,
    CHUPHONG = x.CHUPHONG,
    SOLUONG  = x.SOLUONG };
   rooms_list = room.ToList();
}
MessageBox.Show(rooms_list.Count.ToString());

*未検証。何かがうまくいかなかった場合は教えてください

于 2013-03-20T18:00:15.627 に答える