将SQL记录转换为对象

时间:2017-03-10 05:12:26

标签: c# mysql database linq

目前我正在开发像SharePoint这样的应用程序,我遇到了一个困难,如下所示。

我有一个数据库表来保存我的内容,如下所示

+----+---------------+----------+---------+----------+--------------------------+
| ID |  Content_type |  List_ID |  COL_ID |  ITEM_ID |           VALUE          |
+----+---------------+----------+---------+----------+--------------------------+
|  1 |  "Column"     |        1 |       0 |          |  "ABC"                   |
|  2 |  "Column"     |        1 |       1 |          |  "DEF"                   |
|  3 |  "Item"       |        1 |       0 |        1 |  "<VALUE OF Column ABC>" |
|  4 |  "Item"       |        1 |       1 |        1 |  "<VALUE OF Column DEF>" |
+----+---------------+----------+---------+----------+--------------------------+

我希望使用linq和C#在网上显示这些记录,如下所示....

  ITEM_ID   |ABC                  |DEF
------------+---------------------+----------------------
    1       |<VALUE OF Column ABC>|<VALUE OF Column DEF>

编辑:

我的问题是:

  1. 我想将Column字段中标为content_type的数据库记录用作DataColumn的{​​{1}}。
  2. 我想将数据库中所有记录为DataTable的记录与ITEM的{​​{1}} Item_ID相同。每个数据库记录的值字段将根据DataRow落在DataTable的列上。

1 个答案:

答案 0 :(得分:0)

感谢您的帮助。我自己做了......

  • 首先从DB获取记录,其中content_type =&#34; Column&#34;并使用 这些记录形成一个DataTable
  • 从DB获取所有记录,其中content_typ =&#34; Item&#34;并将每个项目重新分组到ITEM_id =
  • 的列表
  • 映射DataTable的Item.title =列和DataTable行的Items.value =值....

    public static DataTable PopulateRec(int list_id,string web_app,string host_auth) {     DataTable dt = new DataTable();     List Column = Get_Column(list_id,web_app,host_auth);     for(int i = 0; i&lt; Column.Count(); i ++)     {         DataColumn datacolumn = new DataColumn();         datacolumn.ColumnName = Column [i] .ToString();         dt.Columns.Add(的DataColumn);     }     List Items = Get_Item(list_id,web_app,host_auth);     if(Items.Count!= 0)     {         int ItemCount = Convert.ToInt32((来自项目中的Itms)                                          选择Itms.Item_id).Max());         for(int j = 0; j&lt; = ItemCount; j ++)         {             dt.Rows.Add();             列出IndvItem =(来自项目中的Indv                                      其中Indv.Item_id == j                                      选择Indv).ToList();             foreach(IndvItem中的var val)             {                 dt.Rows [j] [val.title] = val.value;             }             IndvItem = null;         }         for(int k = 0; k&lt; dt.Rows.Count; k ++)         {             if(dt.Rows [k] [0] .ToString()== string.Empty)             {                 dt.Rows [K] .Delete();             }         }     }     Column = null;     Items = null;     返回; }

    private static List Get_Column(int list_id,string web_app,string host_auth) {     List content_db = new List();     List columnname = new List();     Config_DB_Context configdb = new Config_DB_Context(&#34; dms_config&#34;);     content_db =(来自configdb.site_mapping中的c                   其中c.host_auth == host_auth                   &安培;&安培; c.web_app == web_app                   select c.content_db)。ToList();     for(int i = 0; i&lt; content_db.Count(); i ++)     {         Content_DB_Context contentdb = new Content_DB_Context(content_db [i]);         columnname =(来自contentdb.content中的c)                           其中c.content_type ==&#34;列&#34;                           &安培;&安培; c.list_id == list_id                           select c.title).ToList();     }     content_db = null;     return columnname; }

    private static List Get_Item(int list_id,string web_app,string host_auth) {     List content_db = new List();     List Itm = new List();     Config_DB_Context configdb = new Config_DB_Context(&#34; dms_config&#34;);     content_db =(来自configdb.site_mapping中的c                   其中c.host_auth == host_auth                   &安培;&安培; c.web_app == web_app                   select c.content_db)。ToList();     for(int i = 0; i&lt; content_db.Count(); i ++)     {         Content_DB_Context contentdb = new Content_DB_Context(content_db [i]);         Itm =(来自contentdb.content中的c                其中c.content_type ==&#34; Item&#34;                &安培;&安培; c.list_id == list_id                选择新的MyItem                {                    col_id =(int)c.column_id,                    list_id =(int)c.list_id,                    title = c.title,                    value = c.value,                    Item_id =(int)c.item_id,                    hidden = c.hidden                })ToList();     }     content_db = null;     返回Itm; }