[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
SolvedPrivate

How to use linq to get top column value of datatable

Posted on 2014-08-26
3
Medium Priority
?
79 Views
Last Modified: 2016-02-26
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
0
Comment
Question by:Scott Baldridge
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
Priya Sudharsan earned 2000 total points
ID: 40287117
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
 

Author Comment

by:Scott Baldridge
ID: 40287674
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
 

Author Comment

by:Scott Baldridge
ID: 40287855
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

Featured Post

Transaction-level recovery for Oracle database

Veeam Explore for Oracle delivers low RTOs and RPOs with agentless transaction log backup and transaction-level recovery of Oracle databases. You can restore the database to a precise point in time, even to a specific transaction.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
zlib is a free compression library (a DLL) on which the popular gzip utility is built.  In this article, we'll see how to use the zlib functions to compress and decompress data in memory; that is, without needing to use a temporary file.  We'll be c…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question