재우니의 블로그

 

 

 

public IEnumerable<IDataRecord> Region
        {
            get
            {
                using (var sqlConnection = new SqlConnection(_connectionString))
                using (var cmd = new SqlCommand("select * from dbo.Region", sqlConnection))
                {
                    sqlConnection.Open();
                    using (var reader = cmd.ExecuteReader())
                        while (reader.Read()) yield return reader as IDataRecord;
                }
            }
        }

 

 

IEnumerable 로 해당 객체 가져옴

 

var regions = context.Region;

 

foreach (var region in regions) {
 Console.WriteLine(region["RegionDescription"]);

}

 

이를 linq 로 조건문이나 정렬을 처리 할 수 있다.

 

string easterRegion = (from region in context.Region
        let regionDescription = region["RegionDescription"].ToString()
        where regionDescription.Contains("Eastern")
        select regionDescription)

   .FirstOrDefault();

 

 

Console.WriteLine(easterRegion);