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

标签 c# asp.net ienumerable

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/

相关文章:

c# - 如何让 System.Net.Http.HttpClient 不遵循 302 重定向?

asp.net - 无法连接到多维数据集

c# - IEnumerable 的可能多重枚举 - 如果我想要多重枚举怎么办?

C# 列表框集合语法

c# - 创建没有任何噪音的透明启动画面

c# - 如何检测UDP数据包丢失? (C#)

c# - 通过 Visual Studio 性能分析,应用程序运行速度更快

c# - 系统.Data.SqlClient.SqlException : Invalid column name 'GenreId'

c# - 使用 MSBuild 构建时如何复制 ASP.NET 项目的内容?

c# - 迭代 IEnumerable<T> 和 List<T> 之间的性能