1

Basically, I need to get the email column from each table in the DataSet (there could be 0 tables in there, or there could be 100) and slap them together into a big List for processing later.

I was about to write the 2x nested loop to do it, but is there an easier way to type this?

My first attempt at loops didn't work so well, since DataTables don't define GetEnumerator :

foreach(DataTable item in emailTables.Tables)
{
    foreach(var email in item)
    {
     // This doesn't work
    }
}
5
  • 1
    easier way? opinion based. Commented Jan 29, 2014 at 23:41
  • Using your current solution, you aren't actually accessing the column, you are accessing the table itself. You would need to do foreach (DataRow row in item.Rows) etc etc, or you could try my LINQ solution below ;) I edited it to reflect your DataSet name. Commented Jan 29, 2014 at 23:53
  • 1
    Read it as "another way" and stop complaining. Commented Jan 29, 2014 at 23:56
  • LINQ seems to be the shortest solution, thanks Evan! Commented Jan 29, 2014 at 23:56
  • Not a problem =) Happy coding! Commented Jan 29, 2014 at 23:58

2 Answers 2

2

Like L.B. said, it's opinion based, but LINQ would be my first choice. A bit less readability but less typing overall and definitely less nesting.

var listEmail = (from DataTable table in emailTables.Tables 
                 from DataRow row in table.Rows 
                 select row["yourColumnNameHere"].ToString()).ToList();

If any of the tables do not (or may not) have an email column, then you will have to do some more validation.

Sign up to request clarification or add additional context in comments.

3 Comments

I'm considering making this into an extension method, would it be possible to extend this code to obtain multiple items, like Email, Name and Address? Maybe into something other than List<>?
You would definitely need a different data structure than List<> (unless you want to do comma separated then String.Split(',') or something. Let me take a look and I will edit if I find a good solution.
@RealityDysfunction I have answered your second question below this one. Take a look.
1

I chose to use a separate answer to extend the question posed in a comment after my initial answer was accepted.

The question was:

I'm considering making this into an extension method, would it be possible to extend this code to obtain multiple items, like Email, Name and Address? Maybe into something other than List<>?

The answer:

Create Anonymous types in a the select statement and assign different values from the columns to those types:

var selectedItems = from DataTable table in emailTables.Tables
            from DataRow row in table.Rows
            select
                new
                {
                    EMail = row["email"].ToString(),
                    Address = row["address"].ToString(),
                    Name = row["name"].ToString()
                };

Then loop through the results in selectedItems and do whatever you would like to the fields. Not sure what type you want to store the results in, but this should give you a pretty good idea.

        foreach (var item in selectedItems)
        {
            //Do whatever you want by accessing the fields EMail, Address, and Name using dot notation like
            var myVar = item.EMail;
            var myVar2 = item.Address;
            //Etc... Not sure what the end result you need is going to be, but you should have a good starting point now.
        }

OR you could just return the selectedItems collection. It's type is IEnumerable<T>.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.