开发者

How i Get Distinct record from DataSet?

I have a Static Datase开发者_JAVA技巧t and i want to Distinct Records from this DataSet how is it possible?


See here: How to select distinct rows in a datatable and store into an array


This link suggests this code to get distinct values from a DataTable:

DataTable distinctTable = originalTable.DefaultView.ToTable(true);

The "true" argument in ToTable() method means it gets distinct values.


As an alternative, you can also go the LINQ route by using the LINQ to DataSet extensions:

using System.Data;

class Program {

  static DataTable dtPosts = new DataTable();

  static void Main(string[] args) {
    //some work here to fill the table, etc.

    //select distinct rows, and only two fields from those rows...
    var rows = (from p in dtPosts.AsEnumerable()
            select new
            {
                Title = p.Field<string>("Title"),
                Body = p.Field<string>("Body")
            }).Distinct();

    Console.WriteLine("Select distinct count = {0}", rows.Count());
    Console.ReadLine();
  }
}

Depends on what you want to do. Thought I add it to the thread. Hope it helps!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜