Skip to content

Instantly share code, notes, and snippets.

@rupakraj
Last active November 26, 2015 07:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rupakraj/5a1d4414fab3f0b26bed to your computer and use it in GitHub Desktop.
Save rupakraj/5a1d4414fab3f0b26bed to your computer and use it in GitHub Desktop.
C# System.Data.DataTable ->List<Object>
Using Select(): a power of LINQ for casting datatable into List of Object
List<Student> target = dt.AsEnumerable()
.Select(row => new Student
{
// assuming column 0's type is Nullable<long>
StudentID = row.Field<long?>(0).GetValueOrDefault(),
StudentName = String.IsNullOrEmpty(row.Field<string>(1)),
}).ToList();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment