• Converting Linq result to XML String


    linq查询得到的结果为Entity,返回给FLex需要转换成XML格式(我目前用的webservice的笨方式),所以需要将Entity转换成XML,本来以为有直接的转换方法,Goo了好久,发现有个老外和我想的一样:先将Entity填充到一个DataSet中,然后利用DataSet的GetXml来返回,呵呵,够麻烦的,应该会有更好的方法,但是我还不知道,先将就着用这个方法了。

            var q = db.Customers.First(p => p.cust_sys_id == id);
            DataTable dt = new DataTable();
            foreach (var col in q.GetType().GetProperties())
                dt.Columns.Add(col.Name, col.PropertyType);
            DataRow dr = dt.NewRow();
            foreach (var col in q.GetType().GetProperties())
                dr[col.Name] = col.GetValue(q, null);
            dt.Rows.Add(dr);
            DataSet ds = new DataSet();
            ds.Tables.Add(dt);
            return ds.GetXml(); 

      

    //into 13/10/2010

      [WebMethod]
        
    public string getCustList()
        {
            var q 
    = from c in db.Customers
                    orderby c.m_date descending
                    select c;
            DataSet ds 
    = new DataSet();
            
    foreach (var t in q)
            {
                DataTable dt 
    = new DataTable();
                
    foreach (var col in t.GetType().GetProperties())
                    dt.Columns.Add(col.Name);
                DataRow r 
    = dt.NewRow();
                
    foreach (var col in t.GetType().GetProperties())
                    r[col.Name] 
    = col.GetValue(t, null);
                dt.Rows.Add(r);
                ds.Tables.Add(dt);
            }
            
    return ds.GetXml();
        } 


  • 相关阅读:
    hdu 5053 the Sum of Cube
    [LeetCode] Word Pattern
    [LeetCode] Minimum Depth of Binary Tree
    [C++] std::vector
    [LeetCode] Count Binary Substrings
    [LeetCode] Degree of an Array
    [LeetCode] String to Integer (atoi)
    [LintCode] 比较字符串
    [LeetCode] Valid Parentheses
    [LeetCode] Perfect Number
  • 原文地址:https://www.cnblogs.com/4kapple/p/1848165.html
Copyright © 2020-2023  润新知