I've got a piece of code that needs to deal with a DataTable. The DataTable looks something like this:
PartnerID | Partner Name | GroupID | Group Name | Description
------------------------------------------------------------------------------------------
1 | First Name | 4 | Group Name1 | Foo
2 | Second Name | 12 | Group Name2 | Bar
3 | Third Name | 7 | Group Name3 | Hello
3 | Third Name | 8 | Group Name4 | Hello World
Now what I am trying to accomplish is the performance of following SQL Statement:
SELECT DISTINCT PartnerID, Partner Name
FROM Table1
In C# using Linq with the expected output looking like this:
PartnerID | Partner Name |
-------------------------------------
1 | First Name |
2 | Second Name |
3 | Third Name |
I have already seen following post:
And found it very unhelpful for my case, since all i really want to do is getting specified columns, but all the answers there display a solution that only seems to be working with where statements or selecting all columns by default.
My current code looks something like this right now:
static void Main(string[] args)
{
DataTable fullTable = new DataTable();
AddColumns(fullTable, "PartnerID", "Partner Name", "GroupID", "Group Name", "Description");
fullTable.Rows.Add(1, "First Name", 4, "Group Name1", "Foo");
fullTable.Rows.Add(2, "Second Name", 12, "Group Name2", "Bar");
fullTable.Rows.Add(3, "Third Name", 7, "Group Name3", "Hello");
fullTable.Rows.Add(3, "Third Name", 8, "Group Name4", "Hello World");
var selectTwoCols = from arow in fullTable.AsEnumerable()
select arow; //how do i select specific columns from those rows?
foreach (DataRow dataRow in selectTwoCols.Rows)
{
foreach (var item in dataRow.ItemArray)
{
Console.Write(item + " ");
}
Console.WriteLine();
}
}
static void AddColumns(DataTable table, params string[] columnNames)
{
for (int i = 0; i < columnNames.Length; i++)
{
table.Columns.Add(columnNames[i]);
}
}
I'm open to using different classes aswell, although im still curious to know how this would be solved using DataTables in particular