开发者

How i Get Distinct record from DataSet?

开发者 https://www.devze.com 2023-01-12 04:41 出处:网络
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 arrayThis link suggest

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

精彩评论

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