Linq to DataSet

本文来自《Pro LINQ IN C# 2010(Adam Freeman and Joseph C.Rattz, Jr.)的第十章,算是自己学习LINQ的笔记,书上说Linq to SQL 只支持 MS SQL Server 数据库, Linq to Dataset,由于是针对Dataset操作,所以没有数据库平台的限制。

下面用代码示例讲解了DataSet的几个操作,代码全部来自于《Pro LINQ IN C# 2010》。为方便实践,DataTable数据不是直接来自于数据库,而是由一个Student数据转化而来。

//Student 类的定义
classStudent
{
public int Id;
public string Name;
}
//GetDataTable函数:Student类数组转DataTable
staticDataTable 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);
}

Distinct 操作

该操作的作用:剔除掉重复的数据行,下面的代码中最重要就是:
IEnumerable<DataRow> distinct =
        dt.AsEnumerable().Distinct(DataRowComparer.Default);

这一行调用Distinct函数返回结果集。

      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);
      IEnumerable<DataRow> distinct =
        dt.AsEnumerable().Distinct(DataRowComparer.Default);

      foreach (DataRow dataRow in distinct)
      {
        Console.WriteLine("{0,-15}{1,-15}",
          dataRow.Field<int>(0),
          dataRow.Field<string>(1));
      }

Except操作

该操作的作用:从Table1中选出那些没有出现在Table2的数据行。

下面代码中最重要的一行就是:
IEnumerable<DataRow> except =
        seq1.Except(seq2, System.Data.DataRowComparer.Default);

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

      foreach (DataRow dataRow in except)
      {
        Console.WriteLine("{0,-15}{1,-15}",
          dataRow.Field<int>(0),
          dataRow.Field<string>(1));
      }

Intersect操作

该操作的作用:checktable1table2中相等的数据行。代码示例如下:

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

      foreach (DataRow dataRow in intersect)
      {
        Console.WriteLine("{0,-15}{1,-15}",
          dataRow.Field<int>(0),
          dataRow.Field<string>(1));
      }

Union操作

该操作的作用:将Table1Table2联合起来。和SQL的关键字Union功能一样。代码示例如下:

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

SequenceEqual操作

该操作的作用:检测table1table2数据表是不是一样的。代码示例如下:

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

原文地址:https://www.cnblogs.com/java20130722/p/3207070.html