Wednesday, August 27, 2014

using LINQ to select from DataTable

Hello, I have the following sample data(below) that is in a datatable. I need to select into a new datatable distinct rows by colname and where the most occurance of Datatype, then if a tie exists with DataType use Length.


I can get the correct data with this sql statement if the data was in a sql database table:



SELECT
*
FROM (
SELECT
*
, ROW_NUMBER() OVER (PARTITION BY Datatype
ORDER BY length DESC) AS w
FROM TN_ImportFileColumns
) x
WHERE w = 1



What I need to have:



ColumnName DataType Length
created datetime 10
phone int 10
name string 4

From this sample data:



ColumnName ExampleData DataType Length
name scott string 4
name ben string 3
name kim string 3
phone 1111111111 int 10
phone 222222 int 6
phone string 0
created 12-31-2014 datetime 10
created 11-09-2013 datetime 10
created 11092013 int 8

Some background info:


The data in the datatable comes from a parse of a text file where I'm trying to determine the most likely datatype for a give row. In my sample data there are 3 columns: name, phone and created. But you see that created has 2 entries for datetime and one for int. Since column(datatype) is most likely datetime then that is the row I need for the given columnName.




newb


No comments:

Post a Comment