How to use linq to get top column value of datatable

Hello, I have a datatable that contains file information such as columnname, datatype and length. I need help constructing a query that will return a row for each distinct coumnname  and the most of a particular data type and length.

Data will be similar to the following except there will be many instances

Name, string, 100
Phone, int,10
Phone, int, 11
Phone, into 10
Created, dateline, 0
Created, string, 20
Name, string, 33
Name, string, 101
Scott BaldridgeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Priya SudharsanProgrammer AnalystCommented:
Do you mean the first occurrence when you say "most"? If yes, add an identity column Seq so that we can group and take the maximum.

var query = yourData
    .GroupBy(x => x.Name,
             (k, g) => g.Aggregate((a, x) => (x.Seq > a.Seq) ? x : a));

// and a quick test...
foreach (var result in query)
{
    Console.WriteLine(result.Name + " " + result.Seq);
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Scott BaldridgeAuthor Commented:
No, not the first occurrence but the max.
 
Perhaps this will help:

From this:
 Name, string, 100
 Name, string, 33
 Name, string, 101
 Phone, int,10
 Phone, int, 11
 Phone, string, 10
 Created, dateline, 0
 Created, dateline, 0
 Created, string, 20
 

I need something link this:
Name, string, 101
Phone, int, 11
Created, dateline, 0

I'm trying to find the most occurrence of a given datatype by name with the length to help break a tie.
0
Scott BaldridgeAuthor Commented:
Perhaps this SQl Statement will help:

SELECT
      *
FROM (
            SELECT
                  *
                , ROW_NUMBER() OVER (PARTITION BY Datatype
                                     ORDER BY length DESC) AS rn
            FROM table1
      ) x
WHERE rn = 1
ORDER BY
       columnName DESC
;
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.