public static class DataConvertor { public static DataTable ToDataTable<T>(IEnumerable<T> data) { PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(typeof(T)); var table = new DataTable(); foreach (PropertyDescriptor prop…
一.List/IEnumerable转换到DataTable/DataView 方法一: /// <summary> /// Convert a List{T} to a DataTable. /// </summary> private DataTable ToDataTable<T>(List<T> items) { var tb = new DataTable(typeof (T).Name); PropertyInfo[]…
使用 FastMember: IEnumerable<SomeType> data = ... DataTable table = new DataTable(); using(var reader = ObjectReader.Create(data)) { table.Load(reader); } This uses the FastMember's meta-programming API for maximum performance. If you want to restrict…
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);…
DataTable dt = GetTestData(10); //获取10条测试数据 var queryByService = from r in dt.AsEnumerable() group r by r.Field<string>(4) into g select new { Service = g.Key, Bookings = g.Count(p => p.Field<string>(1) !=""), ConfirmedBookings =…
问题:我要获得一个角色下对应的所有用户,需要两表连接查询,虽然返回的只有用户数据,但是我想到若是返回的不只是用户数据,而还要加上角色信息,那么我返回什么类型呢,返回var吗,这样不行. 于是我网上找找是否能返回DataTable呢,这样我不用创建中间类了.然后就找到下面的代码:这是别人写的,高手. using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.R…
This uses the FastMember's meta-programming API for maximum performance. If you want to restrict it to particular members (or enforce the order), then you can do that too: IEnumerable<SomeType> data = ... DataTable table = new DataTable(); using(var…
(原文地址:http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/) LINQ查询返回DataTable类型 在使用LINQ查询的时候,一般我们会返回List<T>或IList<T>类型,如下所示: 例1: public List<TSample> GetList() { using (BPDataContext db = newBPDataContext(TCTC_Connectio…
using System.Web.Script.Serialization; using System.Collections.Generic; using System.Reflection; using System.Data; using System; namespace CommonCode { public class Common { /// <summary> /// LINQ返回DataTable类型 /// </summary> public static Da…
首先封装一个公共类,统一来操作RDLC报表 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Xml; using System.Data; using Microsoft.Reporting.WebForms; using System.Text; using System.Collections; using System.IO; using Sy…