23

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.

m.edmondson
  • 30,382
  • 27
  • 123
  • 206

3 Answers3

34

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();
Bryan Watts
  • 44,911
  • 16
  • 83
  • 88
  • Why am I getting 'dataTable does not contain a definition for "Field"'? – Spero Jun 09 '19 at 20:05
  • @Spero: That sounds like a missing extension method. Did you include this? `using System.Data.Linq;` – Bryan Watts Jun 10 '19 at 02:38
  • Thx for the response. I added the necessary assembly and include, but the error is still there. I am pulling the data from DbContext though, not DataSet. I dunno how to use DataSet in Linq yet. – Spero Jun 10 '19 at 09:25
  • I found this:https://stackoverflow.com/questions/27738238/convert-dbcontext-to-datatable-in-code-first-entity-framework Is this necessary? – Spero Jun 10 '19 at 09:28
  • It seems what I am looking for is closer to this question: https://stackoverflow.com/questions/20069154/how-can-i-select-just-two-columns-from-a-list-using-linq – Spero Jun 10 '19 at 10:18
  • @Spero: This answer only applies to usages of `DataSet`. If you are using `DbContext`, there will be ways to do this there. Don't convert your `DbContext` to `DataSet` just to use this answer. – Bryan Watts Jun 10 '19 at 14:46
  • Thx for the suggestion. Tim Schmelter's answer in the other thread worked for me. – Spero Jun 10 '19 at 15:18
12
string[] filePaths = (from DataRow row in yourDataTable.Rows 
                     select row["Filepath"].ToString()).ToArray();
Anthony Pegram
  • 123,721
  • 27
  • 225
  • 246
2

If you want to use LINQ all the way, set up your database and create a context object. Then you should be able to do something like this:

 var filepaths = from order in _context.Orders
                 select order.Filepath;

This is assuming your table for the row is named Orders, which I guess by your first column name of order. If you wanted to return a set of the order numbers as well for using later to know where the file path came from you could do something like so:

var results = from order in _context.Orders
              select new
              {
                  order.OrderNo,
                  order.Filepath
              }

This would give you a new anonymous type that contained both those values as properties.

poindexter12
  • 1,775
  • 1
  • 14
  • 20