3

I have the following ItemArray:

dt.Rows[0].ItemArray.. //{0,1,2,3,4,5}

the headers are : item0,item1,item2 etc..

So far, to get a value from the ItemArray I used to call it by an index.

Is there any way to get the value within the ItemArray with a Linq expression based on the column name?

Thanks

2
  • 1
    You can do without linq by dt.Rows[0]["columnName"].ToString(); Commented Mar 15, 2012 at 8:35
  • Yes, I just wanted to know how to do it with linq due to code maintance Commented Mar 15, 2012 at 8:37

4 Answers 4

9

You can also use the column-name to get the field value:

int item1 = row.Field<int>("Item1");

You could also use LINQ-to-DataSet:

int[] allItems = (from row in dt.AsEnumerable()
                  select row.Field<int>("Item1")).ToArray();

or in method syntax:

int[] allItems = dt.AsEnumerable().Select(r => r.Field<int>("Item1")).ToArray();
Sign up to request clarification or add additional context in comments.

2 Comments

Hi Tim, thanks for your comment. I don't fully understand the syntax here as I wished to perform this action on a specific row (lets say Rows[0]) how do I see it at your example?
@DavidRasuli: If you only have one row and you only want to get a specific field value, use the first method i've shown(row.Field<int>("Item1")). Otherwise explain what you're trying to achieve.
2

If you use the Item indexer rather than ItemArray, you can access items by column name, regardless of whether you use LINQ or not.

dt.Rows[0]["Column Name"]

Comments

1

Tim Schmelter's answer is probably what you are lookin for, just to add also this way using Convert class instead of DataRow.Field:

var q = (from row in dataTable.AsEnumerable() select Convert.ToInt16(row["COLUMN1"])).ToArray();

Comments

0

Here's what I've come up with today solving a similar problem. In my case: (1)I needed to xtract the values from columns named Item1, Item2, ... of bool type.

(2) I needed to xtract the ordinal number of that ItemN that had a true value.

var itemValues = dataTable.Select().Select(
                r => r.ItemArray.Where((c, i) =>
                dataTable.Columns[i].ColumnName.StartsWith("Item") && c is bool)
                .Select((v, i) => new { Index = i + 1, Value = v.ToString().ToBoolean() }))
                                        .ToList();


  if (itemValues.Any())
  {
        //int[] of indices for true values
        var trueIndexArray = itemValues.First().Where(v => v.Value == true)
                    .Select(v => v.Index).ToArray();
  }

forgot an essential part: I have a .ToBoolean() helper extension method to parse object values:

    public static bool ToBoolean(this string s)
    {
        if (bool.TryParse(s, out bool result))
        {
            return result;
        }

        return false;
    }

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.