public IEnumerable GetAddress()
{
DataSet ds = DataOps.GetDataSet(string.Format(" select * from Students"));
DataTable dt = ds.Tables[0];
// What goes here?
}
我需要使用 IEnumerable 方法
我如何返回包含所有只有地址的学生的数据行枚举?
最佳答案
我不知道你的学生类长什么样,但这是一个模型
private IEnumerable<Student> GetAddress()
{
DataSet ds = DataOps.GetDataSet(string.Format(" select * from Students Where NOT NULL [address]"));
DataTable dt = ds.Tables[0];
foreach (DataRow row in dt.Rows)
{
yield return new Student
{
StudentName = row["StudentName "].ToString(),
Address= row["Address"].ToString()
};
}
}
这应该让您知道从这里到哪里去。
关于C#:当字段不为空时返回 IEnumerable?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5877600/