Select single column from dataset with LINQ
Asked Answered
T

3

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.

Tiro answered 13/12, 2010 at 20:49 Comment(0)
R
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();
Rhonarhonchus answered 13/12, 2010 at 20:56 Comment(7)
Why am I getting 'dataTable does not contain a definition for "Field"'?Agram
@Spero: That sounds like a missing extension method. Did you include this? using System.Data.Linq;Rhonarhonchus
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.Agram
I found this:#27738738 Is this necessary?Agram
It seems what I am looking for is closer to this question: #20069654Agram
@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.Rhonarhonchus
Thx for the suggestion. Tim Schmelter's answer in the other thread worked for me.Agram
A
12
string[] filePaths = (from DataRow row in yourDataTable.Rows 
                     select row["Filepath"].ToString()).ToArray();
Admiration answered 13/12, 2010 at 20:51 Comment(5)
This isn't using LINQ at all.Bowl
@Bowl - What? Sure looks like LINQ to me.Bottrop
@jfar: This is technically LINQ syntax, so I removed my down vote. However, in the spirit the question, I think there was a little more that could be explained.Bowl
@poindexter, the OP asked how to utilize LINQ to query his dataset, he did not ask how to completely revamp his data access strategy. This is certainly a classic use of LINQ-to-Objects. You would have him go to a LINQ-to-Entities (or equivalent) model.Admiration
I agree on all counts, I would have him go LINQ to Sql.Bowl
B
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.

Bowl answered 13/12, 2010 at 20:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.