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!
精彩评论