简体   繁体   中英

How do I select a single grouped column from a dataview?

I have a data.table tblWorkList with multiple columns: RecordNr , GroupNum , Section , SubscriberID , and quite a few others.

What I need to do is create a dataview or second datatable that is the equivalent of:

SELECT SubscriberID FROM tblWorkList GROUP BY SubscriberID;

I'm doing it in the application because I need this to end up in a dataview that will then be filtered based on multiple user inputs. I have that part working. I've spent several hours now beating my head against the inte.net trying to figure out how to do this, but I keep running up against errors in solutions that LOOK like they should work but end up failing spectacularly. Although, that said, I'm VERY inexperienced with LINQ right now, so I'm sure I'm missing something pretty straightforward.

(The basic functionality is this: The table contains a list of records to be processed. Basically, I need to take the table full of records, pull the subscriber IDs into a dataview, allow the user to filter that dataview down by a variety of methods (and providing the user a running count of the number of SubscriberID's matching the selected criteria), and when they're done, assign all of the records associated with the resulting SubscriberID collection to a specific analyst to be processed.)

All of the methods I've attempted to use to create the list or dataview of SubscriberID values are enclosed in this:

using (DataTable dt = dsWorkData.Tables["tblWorkData"])

The table tblWorkData contains approximately 23,000 records.

Here are several of my attempts.

Attempt 1 - Error is

Parameter may not be null. Parameter: source'

var result1 = from row in dt.AsEnumerable()
              group row by row.Field<string>("SubscriberID") into grp
              select new { SubscriberID = grp.Key };

ShowMessage(result1.Count().ToString());

Attempt 2 - Error is

'Cannot implicitly convert anonymous type: string SubscriberID to DataRow'

EnumerableRowCollection<DataRow> query =
    from row in dt.AsEnumerable()
    group row by row.Field<string>("SubscriberID") into grp
    select new { SubscriberID = grp.Key };

Attempt 3 - Error is

'The [third] name 'row' does not exist in the current context.'

EnumerableRowCollection<DataRow> query2 =
    from row in dt.AsEnumerable()
    group row by row.Field<string>("SubscriberID") into grp
    select row;

Attempt 4 - same error as Attempt 1:

DataTable newDt = dt.AsEnumerable()
    .GroupBy(r => new { SubscriberID = r["SubscriberID"] })
    .Select(g => g.OrderBy(r => r["SubscriberID"]).First())
    .CopyToDataTable();

MessageBox.Show(newDt.Rows.Count.ToString());

Attempt 5 - same error as Attempt 1:

var result = dt.AsEnumerable().GroupBy(row => row.Field<string>("SubscriberID"));

MessageBox.Show(result.Count().ToString());

Attempt 6 - same error as Attempt 1:

var results = dt.AsEnumerable().GroupBy(g => g["SubscriberID"])
                                          .Select(x => x.First());
MessageBox.Show(results.Count().ToString());

So can someone explain what I'm doing wrong here, or at least point me in the right direction? I don't really care WHICH approach gets used, for the record, as long as there's a way to do this.

Answer was a pair of comments from NetMage:

Your SQL query is really using GROUP BY to do DISTINCT , so just use the LINQ Distinct : dt.AsEnumerable().Select(r => r.Field<string>("SubscriberID") ).Distinct() .

PS Your first error implies that dt is null - source is the parameter name to AsEnumerable .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM