如何将一个通过LINQ查找出来并重新构造的匿名对象转化成DATATABLE

如题:
    DataTable resulttable=null;
    IQueryable <DataRow> result=from table in tabledata
                              where table.field1='0215'
                              select new {字段1=table.field2+"-"+table.field4,字段2=table.field3};
  resulttable = result.CopyToDataTable <DataRow>();

  如上代码,但是转化出错,要如何转化?

 

using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Text;
using  System.Data;
using  System.ComponentModel;

namespace  Test
{
    
public   class  People
    {
        
public   string  Name{ get  ; set ;}
        
public   string  City {  get set ; }
        
public   string  Sex {  get set ; }
    }

    
public   static   class  Extension
    {
        
public   static  DataTable CopyToDataTable < T > ( this  IEnumerable < T >  array)
        {
            var ret 
=   new  DataTable();
            
foreach  (PropertyDescriptor dp  in  TypeDescriptor.GetProperties( typeof (T)))
               
//  if (!dp.IsReadOnly)
                    ret.Columns.Add(dp.Name, dp.PropertyType);
            
foreach  (T item  in  array)
            {
                var Row 
=  ret.NewRow();
                
foreach  (PropertyDescriptor dp  in  TypeDescriptor.GetProperties( typeof (T)))
                   
//  if (!dp.IsReadOnly)
                        Row[dp.Name]  =  dp.GetValue(item);
                ret.Rows.Add(Row);
            }
            
return  ret;
        }

    }

    
    
public   class  Linq2DataTable
    {
        
private   static  List < People >  GetData()
        {
            List
< People >  list  =   new  List < People > ();
            list.Add(
new  People {Name = " Bruce " ,City  = " SZ " ,Sex  = " Man " });
            list.Add(
new  People { Name  =   " John " , City  =   " GZ " , Sex  =   " Man "  });
            list.Add(
new  People { Name  =   " Judy " , City  =   " CS " , Sex  =   " Woman "  });
            
return  list;
        }

        
public   static   void  Test()
        {
            List
< People >  data =  GetData();
            var result 
=  from p  in  data  where  p.Sex  ==   " Man "  select   new  { Name  =  p.Name, City  =  p.City };
            DataTable dt 
=  Extension.CopyToDataTable(result);
            Console.WriteLine(ToString(dt));
        }

        
public   static   string  ToString(System.Data.DataTable data)
        {
            StringBuilder sb 
=   new  StringBuilder();
            
int  colCount  =  data.Columns.Count;
            
foreach  (System.Data.DataRow dr  in  data.Rows)
            {
                
for  ( int  i  =   0 ; i  <  colCount; i ++ )
                {
                    
string  s  =  (dr[i]  ==   null   ?   string .Empty : dr[i].ToString());
                    sb.Append(s);
                    sb.Append(
" , " );
                }
                sb.AppendLine();
            }
            
return  sb.ToString();
        }
    }
}

转载于:https://www.cnblogs.com/lilikang/archive/2008/10/21/1316212.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值