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…
double d=0;if(!Convert.IsDBNull(DataTable.Rows[i][m])){    string str=DataTable.Rows[i][m].ToString().Trim();    if(!String.IsNullOrEmpty(str))  //非空字符串         if(Double.TryParse(str,out d))  //d为正确的数字             d*=0.1; }//此时的d可以赋值给其它了 DBNull:DBNu…
public static DataTable Join(DataTable First, DataTable Second, DataColumn[] FJC, DataColumn[] SJC) { DataTable table = new DataTable("Join"); using (DataSet ds = new DataSet()) { ds.Tables.AddRange(new DataTable[] { First.Copy(), Second.Copy()…
在web开发过程中,有时候为了数据传输的方便,比如:后台需要更新前端的ViewModel,此时我们定义一个与前端ViewModel结构一样的DTO对象,从数据层获取数据后,将数据封装成DTO然后序列化为json传回前端,由于我正在开发的项目中的Model是用DataSet来实现的,不是纯粹的面向对象(如果Model是对象的话可以用AutoMapper来实现转换),所以从数据层获取的都是DataSet或DataTable,这时需要将DataTable转换为DTO对象,DTO对象的属性与DataTa…
介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTable/DataView 方法一: /// <summary> /// Convert a List{T} to a DataTable. /// </summary> private DataTable ToDataTable<T>(List<T> items…
个人感觉Linq实用灵活性很大,参考一篇大牛的文章LINQ查询返回DataTable类型 http://xuzhihong1987.blog.163.com/blog/static/26731587201101853740294/ 附上自己写的一个测试程序源代码. 下载 //创建自定义DataTable String[] _sFiled = new String[] { "ID", "PC", "EPC", "CRC", &q…
一.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 =…
动态绑定ReportViewer虽然之前实现过,但现在弄起来还是有点晕,主要是过去没有使用Linq,数据的操作经常用到DataTable,可以直接拿来使用,现在用Linq更方便,也懒得再用之前的数据库连接方式,幸好,最后发现了这篇文章,将linq和datatable连接了起来,因此,我有了前一篇文章和这篇文章这两大法宝,现在可以轻松搞定动态查询生成报表了,不知道网上有没有使用同样方法的人~~~~~ 在使用LINQ查询的时候,一般我们会返回List<T>或IList<T>类型,如下所…
相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是干什么的呢?有什么区别?数组和List集合为什么可以遍历而其他某些类型或对象不能遍历?它们之间有什么联系呢? 针对这么多疑问,我本来是想来写一篇文章来记录下的,但我在网上又发现了一篇写的很好的文章,对它们之间的关系讲的很详细,我觉得就算我写的话,不见得写的比这篇文章好 :-).以下是文章链接 htt…
DataTable dataTable;       DataView dataView = dataTable.DefaultView;       DataTable dataTableDistinct = dataView.ToTable(true,"FieldName1","FieldName2","...");//注:其中ToTable()的第一个参数为是否DISTINCT   有时候我们需要对数据表进行筛选,微软为我们封装了一个公共方…
新建空Table添加行和列 DataTable dt = new DataTable(); //创建空DataTable 1.添加列 dt.Columns.Add("序号", typeof(string));   //添加列dt.Columns.Add("编号", typeof(string));dt.Columns.Add("名称", typeof(string));dt.Columns.Add("描述", typeof(s…
问题:我要获得一个角色下对应的所有用户,需要两表连接查询,虽然返回的只有用户数据,但是我想到若是返回的不只是用户数据,而还要加上角色信息,那么我返回什么类型呢,返回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…
C# DataTable 详解 dataTable.Rows.Count == 0 //判断DataTable 为空 循环执行dataTable数据 DataTable dtSelect = (DataTable)this.ucGrid_main.DataSource; string sql = string.Empty; for (int i = 0; i < dtSelect.Rows.Count; i++) { DataRow dr = dtSelect.Rows[i]; if (dr[&qu…
自存,此方法可以防止出现DataSet不支持System.Nullable的错误 public static DataTable ToDataTable<T>(IEnumerable<T> varlist) { DataTable dtReturn = new DataTable(); // column names PropertyInfo[] oProps = null; if (varlist == null) return dtReturn; foreach (T rec…
public static DataTable ToDataTable<T>(this IEnumerable<T> varlist) { DataTable dtReturn = new DataTable(); // column names PropertyInfo[] oProps = null; if (varlist == null) return dtReturn; foreach (T rec in varlist) { // Use reflection to g…
[IEnumerator] 用于遍历一个对象,IEnumerator在System.Collections命名空间中. public interface IEnumerator { object Current { get; } bool MoveNext (); void Reset (); } [IEnumerable] 用于遍历一个对象是否能被遍历,IEnumerable在System.Collections命名空间中. public interface IEnumerable { IEn…
C# DataTable 和List之间相互转换的方法 好库文章 » 软件开发 » .NET » C# 发布者:好饱  发布日期:2013-1-27 22:17:49   更新日期:2013-1-27 22:18:55 阅读次数:9554 评分:4.80   介绍:List/IEnumerable转换到DataTable/DataView,以及DataTable转换到List 正文: 一.List<T>/IEnumerable转换到DataTable/DataView 方法一: 1 2 3 4…
(原文地址: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…
在C#的数据表格DataTable操作过程中,有时候在操作DataTable前需要判断DataTable中的值是否存在,此时首选需要判断DataTable是否为null值,而后在判断DataTable中的数据行有多少个. 例如有个DataTable类型的数据变量dataDt,需要判断该DataTable中是否含有值可使用下列程序语句: string ishasValue = ""; if (dataDt == null) { ishasValue = "DataTable为空…
来源:https://www.cnblogs.com/shiyh/p/7478241.html 一.List<T>/IEnumerable转换到DataTable/DataView 方法一: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54…
在实际工作中,当需要进行大批量查询和生成报表的时候,可以使用我写的类. 特点: 无需报表设计器.无需为报表设置数据集 只需要传入查询结果就可以全自动生成报表,传入的对象为Dynamic(目前支持DataTable和IEnumable<T>的传入参数) 文字.数据表可以无限添加 支持图表 ( 2014-5-28 v0.4 增加参数类,完成图表显示功能) 支持数据分组(2014-5-19 v0.3 添加表格内分组) 我没有采用使用操纵微软报表Schema的方法,而是用了拼接字符串:( 将来想到的扩…
反射Reflection,MFC时代叫RTTI(Runtime Type Identification) 运行时类型识别,提供一种动态创建对象的能力. 这里不谈反射的概念和基本用法,仅仅就我遇到的ERP系统中,有哪些地方用到了反射,是如何用的. 1  操作对象的属性或方法  Get/Set property and invoke method 通过反射调用,代码中很容易形成抽象化的公共代码,比如,系统中很多地方,直接用反射对对象赋值,参考: ReflectionHelper.SetPropert…
http://www.cnblogs.com/SkySoot/archive/2012/08/21/2649471.html DataTable.Select()方法使用和 SQL 相似的过滤语法从 DataTable 中提取你关心的记录,虽然 Select()可以很好的工作,但它还是有一些明显的限制.首先,它是基于字符串的,也就是说可能的错误不能在编译的时候发现.其次,它的过滤功能也很有限,它没有提供 LINQ 操作符能够提供的其他特性,如排序.分组以及投影. 使用 LINQ to DataS…
首先封装一个公共类,统一来操作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…
利用Aspose.Cells导出excel 注意的问题 1.DataTable的处理 2.进行编码,便于中文名文件下载 3.别忘了Aspose.Cells.dll(可以自己在网上搜索) public static bool DataTableToExcel2(DataTable datatable, string filepath, out string error) { error = ""; Aspose.Cells.Workbook wb = new Aspose.Cells.W…