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

  • 相关阅读:
    编程珠玑(续) 读书笔记 -(第三章 程序员的忏悔)
    java for循环
    java 中的 instanceof
    大脑学习
    voa 2015.4.29
    编程珠玑(续) 读书笔记 -(前言+第一章性能监视工具)
    voa 2015 / 4 / 27
    voa 2015 / 4 / 26
    背包问题 算法实现
    LCS 算法实现
  • 原文地址:https://www.cnblogs.com/yetiea/p/2072023.html
Copyright © 2011-2022 走看看