Linq to Dataset

class Student
{
public int Id;
public 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);
}


The OutputDataTableHeader Method

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

---------------------
Distinct

The Distinct Prototype
public static IEnumerable<T> Distinct<T> (
this IEnumerable<T> source,
IEqualityComparer<T> comparer);


Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 6, Name = "Ulyses Hutchens" },
new Student { Id = 19, Name = "Bob Tanko" },
new Student { Id = 45, Name = "Erin Doutensal" },
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 12, Name = "Bob Mapplethorpe" },
new Student { Id = 17, Name = "Anthony Adams" },
new Student { Id = 32, Name = "Dignan Stephens" }
};
DataTable dt = GetDataTable(students);
Console.WriteLine("{0}Before calling Distinct(){0}",
System.Environment.NewLine);
OutputDataTableHeader(dt, 15);
foreach (DataRow dataRow in dt.Rows)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}
IEnumerable<DataRow> distinct =
dt.AsEnumerable().Distinct(DataRowComparer.Default);
Console.WriteLine("{0}After calling Distinct(){0}",
System.Environment.NewLine);
OutputDataTableHeader(dt, 15);
foreach (DataRow dataRow in distinct)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}

Except

The Except Prototype
public static IEnumerable<T> Except<T> (
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer);

Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};
Student[] students2 = {
new Student { Id = 5, Name = "Abe Henry" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 29, Name = "Future Man" },
new Student { Id = 72, Name = "Dignan Stephens" }
};

DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable(students2);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
IEnumerable<DataRow> except =
seq1.Except(seq2, System.Data.DataRowComparer.Default);
Console.WriteLine("{0}Results of Except() with comparer{0}",
System.Environment.NewLine);
OutputDataTableHeader(dt1, 15);
foreach (DataRow dataRow in except)
{
Console.WriteLine("{0,-15}{1,-15}",

dataRow.Field<int>(0),
dataRow.Field<string>(1));
}
except = seq1.Except(seq2);
Console.WriteLine("{0}Results of Except() without comparer{0}",
System.Environment.NewLine);
OutputDataTableHeader(dt1, 15);
foreach (DataRow dataRow in except)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}

Results of Except() with comparer
Id Name
==============================
1 Joe Rattz
13 Stacy Sinclair
Results of Except() without comparer
Id Name
==============================
1 Joe Rattz
7 Anthony Adams
13 Stacy Sinclair
72 Dignan Stephens
--------------------------------------------------------------------
Intersect
The Intersect Prototype
public static IEnumerable<T> Intersect<T> (
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer);



Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};
Student[] students2 = {
new Student { Id = 5, Name = "Abe Henry" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 29, Name = "Future Man" },
new Student { Id = 72, Name = "Dignan Stephens" }
};


DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable(students2);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
IEnumerable<DataRow> intersect =
seq1.Intersect(seq2, System.Data.DataRowComparer.Default);

Console.WriteLine("{0}Results of Intersect() with comparer{0}",
System.Environment.NewLine);
OutputDataTableHeader(dt1, 15);
foreach (DataRow dataRow in intersect)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}
intersect = seq1.Intersect(seq2);
Console.WriteLine("{0}Results of Intersect() without comparer{0}",
System.Environment.NewLine);
OutputDataTableHeader(dt1, 15);
foreach (DataRow dataRow in intersect)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}


Results of Intersect() with comparer
Id Name
==============================
7 Anthony Adams
72 Dignan Stephens
Results of Intersect() without comparer
Id Name
==============================

------------------------------------------------
Union

The Union Prototype
public static IEnumerable<T> Union<T> (
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer);


Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};
Student[] students2 = {
new Student { Id = 5, Name = "Abe Henry" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 29, Name = "Future Man" },
new Student { Id = 72, Name = "Dignan Stephens" }
};

DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable(students2);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
IEnumerable<DataRow> union =
seq1.Union(seq2, System.Data.DataRowComparer.Default);
Console.WriteLine("{0}Results of Union() with comparer{0}",
System.Environment.NewLine);


OutputDataTableHeader(dt1, 15);
foreach (DataRow dataRow in union)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}
union = seq1.Union(seq2);
Console.WriteLine("{0}Results of Union() without comparer{0}",
System.Environment.NewLine);
OutputDataTableHeader(dt1, 15);
foreach (DataRow dataRow in union)
{
Console.WriteLine("{0,-15}{1,-15}",
dataRow.Field<int>(0),
dataRow.Field<string>(1));
}


Results of Union() with comparer
Id Name
==============================
1 Joe Rattz
7 Anthony Adams
13 Stacy Sinclair
72 Dignan Stephens
5 Abe Henry
29 Future Man
Results of Union() without comparer
Id Name
==============================
1 Joe Rattz
7 Anthony Adams
13 Stacy Sinclair
72 Dignan Stephens
5 Abe Henry
7 Anthony Adams
29 Future Man
72 Dignan Stephens
Notice that the results of the Union 


--------------------------------------------------------

SequenceEqual

public static bool SequenceEqual<T> (
this IEnumerable<T> first,
IEnumerable<T> second,
IEqualityComparer<T> comparer);


Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};
DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable(students);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
bool equal = seq1.SequenceEqual(seq2, System.Data.DataRowComparer.Default);
Console.WriteLine("SequenceEqual() with comparer : {0}", equal);

equal = seq1.SequenceEqual(seq2);
Console.WriteLine("SequenceEqual() without comparer : {0}", equal);

SequenceEqual() with comparer : True
SequenceEqual() without comparer : False

----------------------------------------------------------
Console.WriteLine("(3 == 3) is {0}.", (3 == 3));
(3 == 3) is True.
Console.WriteLine("((Object)3 == (Object)3) is {0}.", ((Object)3 == (Object)3));
((Object)3 == (Object)3) is False.


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


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



Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};

StudentClass[] classDesignations = {
new StudentClass { Id = 1, Class = "Sophmore" },
new StudentClass { Id = 7, Class = "Freshman" },
new StudentClass { Id = 13, Class = "Graduate" },
new StudentClass { Id = 72, Class = "Senior" }
};
DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable2(classDesignations);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
string anthonysClass = (from s in seq1
where s.Field<string>("Name") == "Anthony Adams"
from c in seq2
where c["Id"] == s["Id"]
select (string)c["Class"]).
SingleOrDefault<string>();
Console.WriteLine("Anthony's Class is: {0}",
anthonysClass != null ? anthonysClass : "null");


//Anthony's Class is: null


-----------------------------------------------


Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};



StudentClass[] classDesignations = {
new StudentClass { Id = 1, Class = "Sophmore" },
new StudentClass { Id = 7, Class = "Freshman" },
new StudentClass { Id = 13, Class = "Graduate" },
new StudentClass { Id = 72, Class = "Senior" }
};
DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable2(classDesignations);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
string anthonysClass = (from s in seq1
where s.Field<string>("Name") == "Anthony Adams"
from c in seq2
where (int)c["Id"] == (int)s["Id"]
select (string)c["Class"]).
SingleOrDefault<string>();
Console.WriteLine("Anthony's Class is: {0}",
anthonysClass != null ? anthonysClass : "null");


//Anthony's Class is: Freshman

--------------------------------------------

Student[] students = {
new Student { Id = 1, Name = "Joe Rattz" },
new Student { Id = 7, Name = "Anthony Adams" },
new Student { Id = 13, Name = "Stacy Sinclair" },
new Student { Id = 72, Name = "Dignan Stephens" }
};
StudentClass[] classDesignations = {
new StudentClass { Id = 1, Class = "Sophmore" },
new StudentClass { Id = 7, Class = "Freshman" },
new StudentClass { Id = 13, Class = "Graduate" },
new StudentClass { Id = 72, Class = "Senior" }
};

DataTable dt1 = GetDataTable(students);
IEnumerable<DataRow> seq1 = dt1.AsEnumerable();
DataTable dt2 = GetDataTable2(classDesignations);
IEnumerable<DataRow> seq2 = dt2.AsEnumerable();
string anthonysClass = (from s in seq1
where s.Field<string>("Name") == "Anthony Adams"
from c in seq2
where c.Field<int>("Id") == s.Field<int>("Id")
select (string)c["Class"]).
SingleOrDefault<string>();
Console.WriteLine("Anthony's Class is: {0}",
anthonysClass != null ? anthonysClass : "null");

Anthony's Class is: Freshman




Ebook Download
View all
Learn
View all