我通过使用select语句LINQ to SQL从我的表中选择了4列。然后我创建一个新的List来存储查询结果。但是,代码 room.ToList(); 是错误的。
为什么我们不能使用“rooms_list = room.ToList()”?
我们该如何解决问题?
感谢教我!
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());
}
答案 0 :(得分: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());
*未经测试。如果有什么不起作用,请告诉我