locked
Create an Array using LINQ RRS feed

  • Question

  • I have a table containing a number of records. The records have an ID number i.e. H1, H2, H3. Each record also contains a field called C1 which contains a value.

    There can be multiple records with the same ID.

    I want to read all the values of fields C1 for records with the ID H1 into an array. I don't know exactly how many records there are with the ID H1.

    Does anyone know how I would create an array holding the values?

    Thursday, October 1, 2015 8:45 AM

All replies

  • Hi JMcCon,

    First of all why this question in CRM Development?
    This should be somwhere in C#.

    Anyways below code would help you, Please check:


                #region Sample Table
                DataTable table = new DataTable();
                table.Columns.Add("id", typeof(string));
                table.Columns.Add("C1", typeof(string));
                table.Columns.Add("Date", typeof(DateTime));
                table.Rows.Add("H1", "Indocin", DateTime.Now);
                table.Rows.Add("H2", "Enebrel", DateTime.Now);
                table.Rows.Add("H3", "Hydralazine", DateTime.Now);
                table.Rows.Add("H2", "Combivent", DateTime.Now);
                table.Rows.Add("H2", "Dilantin", DateTime.Now);
                #endregion

                var C1_value_for_H2 = from t in table.AsEnumerable()
                                      where t.Field<string>(0).Equals("H2")
                                      select t.Field<string>("C1");
    • Proposed as answer by Piyush Parate Thursday, October 1, 2015 10:07 AM
    Thursday, October 1, 2015 9:52 AM