CSharp - LINQ SQL Field<T>

Introduction

Field<T> operator can obtain the value of a column from a DataRow object and handle the casting of DBNull.Value and boxed value comparison.

Prototypes

The Field operator has six prototypes we cover.

The first prototype returns the column's value for the DataColumn and version specified.

public static T Field (
this DataRow first,
     System.Data.DataColumn column,
     System.Data.DataRowVersion version);

The second prototype returns the column's value for the column with the name and version specified.

public static T Field (
this DataRow first,
     string columnName,
     System.Data.DataRowVersion version);

The third prototype returns the column's value for the column with the ordinal and version specified.

public static T Field (
this DataRow first,
     int ordinal,
     System.Data.DataRowVersion version);

The fourth prototype returns the column's current value only for the DataColumn specified.

public static T Field (
  this DataRow first,
  System.Data.DataColumn column);

The fifth prototype returns the column's current value only for the column with the specified name.

public static T Field (
  this DataRow first,
  string columnName);

The sixth prototype returns the column's current value only for the column with the specified ordinal.

public static T Field (
  this DataRow first,
  int ordinal);

using System;
using System.Linq;
using System.Collections;
using System.Collections.Generic;
using System.Data;

class Program
{
    static void Main(string[] args)
    {
        Student[] students = {
          new Student { Id = 1, Name = "Joe Ruby" },
          new Student { Id = 7, Name = "Apache Python" },
          new Student { Id = 13, Name = "Scala CSS" },
          new Student { Id = 72, Name = "Django SQL" }
        };

        DataTable dt1 = GetDataTable(students);
        IEnumerable<DataRow> seq1 = dt1.AsEnumerable();

        int id;

        //  Using prototype 1.

        id = (from s in seq1
              where s.Field<string>("Name") == "Apache Python"
              select s.Field<int>(dt1.Columns[0], DataRowVersion.Current)).
             Single<int>();
        Console.WriteLine("Apache's Id retrieved with prototype 1 is: {0}", id);

        //  Using prototype 2.
        id = (from s in seq1
              where s.Field<string>("Name") == "Apache Python"
              select s.Field<int>("Id", DataRowVersion.Current)).
             Single<int>();
        Console.WriteLine("Apache's Id retrieved with prototype 2 is: {0}", id);

        //  Using prototype 3.
        id = (from s in seq1
              where s.Field<string>("Name") == "Apache Python"
              select s.Field<int>(0, DataRowVersion.Current)).
             Single<int>();
        Console.WriteLine("Apache's Id retrieved with prototype 3 is: {0}", id);

        //  Using prototype 4.
        id = (from s in seq1
              where s.Field<string>("Name") == "Apache Python"
              select s.Field<int>(dt1.Columns[0])).
             Single<int>();
        Console.WriteLine("Apache's Id retrieved with prototype 4 is: {0}", id);

        //  Using prototype 5.
        id = (from s in seq1
              where s.Field<string>("Name") == "Apache Python"
              select s.Field<int>("Id")).
             Single<int>();
        Console.WriteLine("Apache's Id retrieved with prototype 5 is: {0}", id);

        //  Using prototype 6.
        id = (from s in seq1
              where s.Field<string>("Name") == "Apache Python"
              select s.Field<int>(0)).
             Single<int>();
        Console.WriteLine("Apache's Id retrieved with prototype 6 is: {0}", id);



    }
    static DataTable GetDataTable(Student[] students)
    {
        DataTable table = new DataTable();

        table.Columns.Add("Id", typeof(Int32));
        table.Columns.Add("Name", typeof(string));

        foreach (Student student in students)
        {
            table.Rows.Add(student.Id, student.Name);
        }

        return (table);
    }
    static void OutputDataTableHeader(DataTable dt, int columnWidth)
    {
        string format = string.Format("{0}0,-{1}{2}", "{", columnWidth, "}");

        //  Display the column headings.
        foreach (DataColumn column in dt.Columns)
        {
            Console.Write(format, column.ColumnName);
        }
        Console.WriteLine();
        foreach (DataColumn column in dt.Columns)
        {
            for (int i = 0; i < columnWidth; i++)
            {
                Console.Write("=");
            }
        }
        Console.WriteLine();
    }
    static DataTable GetDataTable2(StudentClass[] studentClasses)
    {
        DataTable table = new DataTable();

        table.Columns.Add("Id", typeof(Int32));
        table.Columns.Add("Class", typeof(string));

        foreach (StudentClass studentClass in studentClasses)
        {
            table.Rows.Add(studentClass.Id, studentClass.Class);
        }

        return (table);
    }

}

class StudentClass
{
    public int Id;
    public string Class;
}
class Student
{
    public int Id;
    public string Name;
}

Related Topics