Whiz Kid
Whiz Kid

Reputation: 89

LINQ IEnumerable DataRow Select

I've been using following LINQ for querying records from IEnumerable(DataRow). Instead of hardcoding field names in Linq, I want to pass string[] as input parameter and field names needs to be retrieved from string[]. Please assist me on this.

List<string[]> IDcolls = drResults.Select(q => new[] 
                                { 
                                     q["empid"].ToString(),  
                                     q["empname"].ToString() 
                                })
                                .Skip(mBatch * batchSize)
                                .Take(batchSize)
                                .ToList();

string[] IDs = (from q in drResults
                select q["empid"].ToString())
                .Skip(i * batchSize)
                .Take(batchSize)
                .ToArray();

Upvotes: 1

Views: 469

Answers (1)

Ivan Stoev
Ivan Stoev

Reputation: 205629

Something like this should do the job:

string[] columns = ...;

var result = drResults
    .Skip(i * batchSize)
    .Take(batchSize)
    .Select(dr => columns.Select(c => dr[c].ToString()).ToArray())
    .ToList();

Upvotes: 2

Related Questions