Question

        int numPics = 3; //Populated from a query
        string[] picID = new string[numPics];
        //pictureTable is constructed so that it correlates to pictureTable[column][row]
        string[][] pictureTable = null; //assume the table has data
        for (int i = 0; i < numPics; i++)
        {
            //LINQ query doesn't work. Returns an IEnumerable<string> instead of a string.
            picID[i] = pictureTable.Where(p => p[0].Equals("ID")).Select(p => p[i]);
        }

I am new to LINQ, but I've been searching and haven't found an answer. I want to be able to check the first string of every column in my pictureTable using LINQ to see if it matches a string. Then, I want to take that column and extract data from each of the rows from 0 to i. I understand I can do it with a for loop by changing the column and keeping the row the same, but I want to use LINQ to achieve the same result.

Also if it is possible to get rid of the first for loop and achieve the same result, I would really be interested in that as well.

EDIT: Lets say we have a table that has the following data, keep in mind everything is a string.

Column Name    [ID]  [Name]  [Age]
Row 1           [1]   [Jim]   [25]   
Row 2           [2]   [Bob]   [30]
Row 3           [3]   [Joe]   [35]

I want to be able to query a columns name, then be able to get data from it, either by index or querying the row's data. I'll give an example using a for loop that achieves what I want.

        string[][] table = new string[][] { 
                              new string[] { "ID", "Name", "Age" }, 
                              new string[] { "1",  "Jim",  "25" },
                              new string[] { "2",  "Bob",  "30" },
                              new string[] { "3",  "Joe",  "35" }};

        string[] resultRow = new string[table.GetLength(1)];
        for (int i = 0; i < table.GetLength(0); i++)
        {
            if (table[i][0] == "Name") //Given this in a LINQ Query
            {
                Console.WriteLine("Column Name = {0}\n", table[i][0]);
                for (int j = 1; j < table.GetLength(1); j++) //starts at 1
                {
                    resultRow[i] = table[i][j]; //This is what I want to happen.
                }
                break; //exit outer loop
            }
        }
        //Output:
        //Column Name = Name
Was it helpful?

Solution

I think this would give you the equivalent of what you are looking for in your resultRow array

string[][] table = new string[][] { 
    new string[] { "ID", "Name", "Age" }, 
    new string[] { "1", "Jim", "25" },
    new string[] { "2", "Bob", "30" },
    new string[] { "3", "Joe", "35" }
};

//get index of column to look at
string columnName = "Name";
var columnIndex = Array.IndexOf(table[0], columnName, 0);

// skip the title row, and then skip columns until you get to the proper index and get its value
var results = table.Skip(1).Select(row => row.Skip(columnIndex).FirstOrDefault());

foreach (var result in results)
{
    Console.WriteLine(result);
}

One other thing to look at would be SelectMany, as you can use it to flatten mlutiple lists into a single list.

OTHER TIPS

Do you just want to concatenate the strings together? If so you can just do this:

picID[i] = string.Concat(pictureTable.Where(p => p[0].Equals("ID")).Select(p => p[i]));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top