zoukankan      html  css  js  c++  java
  • WCF返回匿名类型(整理自网络以总结备忘)

      在WCF的服务端,通常会有很多函数需要返回匿名类型的集合。
      之前一直在winform或web的类中返回object[]或是List<object>是OK的,但是在wcf里返回的时候会报错。
      试了几种方法,最终通过返回DataTable的方法解决了。
      首先,写个扩展方法用来转换IEnumberable类型的数据为DataTable。

    public static DataTable ConvertToDataTable<T>(this IEnumerable<T> array)
    {
    var ret
    = new DataTable();
    ret.TableName
    = typeof(T).Name.Replace(".", "_");//必须要设置该属性,否则会报错,同webservice返回datatable一样
    foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
    {
    //此处可以添加逻辑将nullable类型单独处理
    ret.Columns.Add(dp.Name, dp.PropertyType);
    }
    foreach (T item in array)
    {
    var Row
    = ret.NewRow();
    foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T)))
    Row[dp.Name]
    = dp.GetValue(item);
    ret.Rows.Add(Row);
    }
    return ret;
    }


      其次,在服务端,查询出匿名类型后,调用上面的方法来进行转换,如下

    public DataTable GetClassInfo()
    {
    try
    {
    using (MyDbcontext entities = new MyDbcontext())
    {
    DataTable dt
    = (from c in entities.V_bookdefault
    orderby c.bookupdatetime descending
    select
    new { c.bookid, c.bookname, c.bookimagepath }).ToList().ConvertToDataTable();

    return dt;
    }
    }
    catch (Exception ex)
    {
    //do sth
    }
    }

      对于匿名类型中nullable属性情况,可以用两种方式解决:

        一、在ConvertToDataTable方法中将每种nullable特殊处理

          二、在匿名对象中将如DateTime? int? ,等类型转化
    总结自:http://hi.baidu.com/kanbafang/blog/item/3e708214f8803c04c83d6da1.html

  • 相关阅读:
    HDU 1284 思维上的水题
    Buy Tickets POJ
    K-th Number Poj
    主席树入门+博客推荐
    Greg and Array CodeForces 296C 差分数组
    三连击 P1008 洛谷 python写法
    Lost Cows POJ 2182 思维+巧法
    Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
    Can you answer these queries? HDU 4027 线段树
    敌兵布阵 HDU 1166 线段树
  • 原文地址:https://www.cnblogs.com/yetiea/p/2072023.html
Copyright © 2011-2022 走看看