CSharp - LINQ SQL SetField<T>

Introduction

SetField<T> operator handles the case where a DataColumn object's value is set with a nullable data type whose value is null.

Prototypes

SetField<T> operator has three prototypes we cover.

The first prototype can set a column's current value for the DataColumn specified.

public static void SetField (
this DataRow first,
     System.Data.DataColumn column,
     T value);

The second prototype can set a column's current value for the column with the specified name.

public static void SetField (
this DataRow first,
     string columnName,
     T value);

The third prototype allows you to set a column's current value for the column with the specified ordinal.

public static void SetField (
this DataRow first,
     int ordinal,
     T value);

Demo

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

class Program//from   w  ww .  jav a  2  s  . c  om
{
    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();

        Console.WriteLine("{0}Results before calling any prototype:",
          System.Environment.NewLine);

        foreach (DataRow dataRow in seq1)
        {
            Console.WriteLine("Student Id = {0} is {1}", dataRow.Field<int>("Id"),
              dataRow.Field<string>("Name"));
        }

        //  Using prototype 1.
        (from s in seq1
         where s.Field<string>("Name") == "Apache Python"
         select s).Single<DataRow>().SetField(dt1.Columns[1], "C++");

        Console.WriteLine("{0}Results after calling prototype 1:",
          System.Environment.NewLine);

        foreach (DataRow dataRow in seq1)
        {
            Console.WriteLine("Student Id = {0} is {1}", dataRow.Field<int>("Id"),
              dataRow.Field<string>("Name"));
        }
        
        //  Using prototype 2.
        (from s in seq1
         where s.Field<string>("Name") == "C++"
         select s).Single<DataRow>().SetField("Name", "new Value");

        Console.WriteLine("{0}Results after calling prototype 2:",
            System.Environment.NewLine);

        foreach (DataRow dataRow in seq1)
        {
            Console.WriteLine("Student Id = {0} is {1}", dataRow.Field<int>("Id"),
              dataRow.Field<string>("Name"));
        }

      //  Using prototype 3.
      (from s in seq1
       where s.Field<string>("Name") == "new Value"
       select s).Single<DataRow>().SetField("Name", "Tony Wonder");

        Console.WriteLine("{0}Results after calling prototype 3:",
          System.Environment.NewLine);

        foreach (DataRow dataRow in seq1)
        {
            Console.WriteLine("Student Id = {0} is {1}", dataRow.Field<int>("Id"),
              dataRow.Field<string>("Name"));
        }

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

}


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

Result