Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select single column from dataset with LINQ

Tags:

Just getting my head around all this LINQ stuff and it seems I'm stuck at the first hurdle.

I have a datatable as such:

OrderNo     LetterGroup Filepath ----------- ----------- -------------------------------------------------- 0           0           Letters/SampleImage.jpg 0           0           Letters/UKPC7_0.jpg 0           0           Letters/UKPC8_0.jpg 

What I need is to get all of the filepaths from the Filepath column into a String array. I thought LINQ would be perfect for this (am I right?), but can't seem to construct the correct query.

Can anyone provide some code samples that would point me in the right direction? I have searched around - but don't seem to be getting anywhere.

like image 951
m.edmondson Avatar asked Dec 13 '10 20:12

m.edmondson


People also ask

How to get single column value from DataTable in c#?

How to get the column value from the data table. Can try as: string countryName = "USA"; DataTable dt = new DataTable(); int id = (from DataRow dr in dt. Rows where (string)dr["CountryName"] == countryName select (int)dr["id"]).


1 Answers

There are extension methods which make working with data sets much easier:

using System.Data.Linq;  var filePaths =     from row in dataTable.AsEnumerable()     select row.Field<string>("Filepath");  var filePathsArray = filePaths.ToArray(); 

You can also use the method syntax to put it in one statement:

var filePaths = dataTable     .AsEnumerable()     .Select(row => row.Field<string>("Filepath"))     .ToArray(); 
like image 183
Bryan Watts Avatar answered Nov 29 '22 08:11

Bryan Watts