NewsFeed   -
KỸ THUẬT LẬP TRÌNH
S  M  L
Another LINQ query convert to datatable -- Tested - C#
 public DataTable ConvertToDataTable(IEnumerable queryResult)
    {
        DataTable dtReturn = new DataTable("Result");

        // column names
        PropertyInfo[] oProps = null;

        if (queryResult == null)
        {
            return dtReturn;
        }

        foreach (T rec in queryResult)
        {
            // Use reflection to get property names, to create table, Only first time, others
            //will follow
            if (oProps == null)
            {
                oProps = ((Type)rec.GetType()).GetProperties();

                foreach (PropertyInfo pi in oProps)
                {
                    Type colType = pi.PropertyType;
                    if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
                    {
                        colType = colType.GetGenericArguments()[0];
                    }
                    dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
                }
            }

            DataRow dr = dtReturn.NewRow();

            foreach (PropertyInfo pi in oProps)
            {
                dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
            }

            dtReturn.Rows.Add(dr);
        }

        return dtReturn;
    }

---------------------- USAGE:
 DataTable dt = ConvertToDataTable(query);
 HttpContext.Current.Session["QUERY"] = dt;

----------------------
Read record(s) from datatable:

string mFilter = "EmailKey='" + ckFixEmail.ToString() + "'";
            DataRow[] filterRows = dt.Select(mFilter);
            if (filterRows.Length > 0)
            {
                this.tb_Name.Text = filterRows[0][3].ToString();
                this.tb_Email.Text = filterRows[0][1].ToString();
                this.ddl_UserTypes.SelectedIndex = ddl_UserTypes.Items.IndexOf(ddl_UserTypes.Items.FindByText(filterRows[0][5].ToString()));
                this.ddl_School.SelectedIndex = ddl_School.Items.IndexOf(ddl_School.Items.FindByText(filterRows[0][7].ToString()));
                //Get grader for: ddl_Grade
            }
- 04/15/2011
Search:
NGHỆ THUẬT - PHIM - ẢNH

HÌNH ẢNH ĐẸP

Photo

Album: Hoa Khôi
< July 2011 >
Sun Mon Tue Wed Thu Fri Sat
26 27 28 29 30 1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31 1 2 3 4 5 6
More... (All Collections)
H2O NEWS

Everything Meaning To You!

Email: [email protected]

Website: emty.org

Liên hệ

Đặt làm trang chủ

@