[英]LINQ to Datatable - How to add an if condition in linq query where clause?
OleDbCommand commandSec = new OleDbCommand();
commandSec.CommandText = "SELECT [SectionName], [Strand], [ReqGrade], [GradeLevel] FROM tbl_section";
OleDbDataAdapter daSec = new OleDbDataAdapter(commandSec);
commandSec.Connection = conn;
DataTable dt = new DataTable();
DataSet ds = new DataSet();
daSec.Fill(dt);
daSec.Fill(ds, "tbl_section");
var section = (from DataRow dr in dt.AsEnumerable()
where (double)dr["ReqGrade"] >= Convert.ToDouble(txt_genave.Text) &&
(string)dr["GradeLevel"] == gradelevel
select (string)dr["SectionName"]).FirstOrDefault();
I get an InvalidCastException: Specified cast it not valid.
我收到一个
InvalidCastException: Specified cast it not valid.
I need to insert a class section to a particular student base on his/her GradeLevel and the ReqGrade. 我需要根据他/她的GradeLevel和ReqGrade为特定学生插入一个班级部分。
I think your problem may be here: 我认为您的问题可能在这里:
from DataRow dr in dt.AsEnumerable()
Try: 尝试:
from DataRow dr in dt.Rows.AsEnumerable()
The DataTable
itself is not enumerable, but the rows within it are. DataTable
本身不是可枚举的,但其中的行是可枚举的。
I was able to reproduce your issue and avoid the error by changing 我能够重现您的问题,并通过更改来避免错误
where (double)dr["ReqGrade"] >= ...
to 至
where Convert.ToDouble(dr["ReqGrade"]) >= ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.