Skip to main content
 首页 » 编程设计

C#:当字段不为空时返回 IEnumerable

2024年02月23日9xxx_UU

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() 
                                      }; 
            } 
 
        } 

这应该让您知道从这里到哪里去。