我不知道这里发生了什么,想知道是否有人可以解决我的问题。 我正在尝试用我的数据库中的< = 2到“Jerry”的teacher_Id替换所有First_names。
foreach标有错误并说:
无法将'string'类型转换为'Project.TeacherDetail'
private void button3_Click(object sender, EventArgs e)
{
ProjectDBDataContext Context = new ProjectDBDataContext();
var r =
from b in Context.TeacherDetails
where b.TeacherId <= 2
select b.First_Name ;
foreach (TeacherDetail b in r)
b.First_Name = "Jerry";
Context.SubmitChanges();
}
任何帮助都会受到很多赞赏
答案 0 :(得分:0)
假设;
public class TeacherDetails
{
public string First_Name { get; set; }
//....
}
你需要像这样实现它;
private void button3_Click(object sender, EventArgs e)
{
ProjectDBDataContext Context = new ProjectDBDataContext();
var r =
from b in Context.TeacherDetails
where b.TeacherId <= 2
select b; //Remove .First_Name here
foreach (TeacherDetail b in r)
b.First_Name = "Jerry";
Context.SubmitChanges();
}
您选择的是string
select b.First_Name
,而您会在TeacherDetail
中将其作为foreach
进行循环。