using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main(string[] args)
{
var nums = GetAllNumber();
//var nums = GetAllNumber().ToList();
var t = nums.Skip(100000).Take(2000).Where(x => x % 7 == 0);
foreach(int i in t)
{
Console.Write("{0}\t", i.ToString());
}
}
static IEnumerable<int> GetAllNumber()
{
for (int i = 0;i<int.MaxValue;i++)
{
yield return i;
}
yield return int.MaxValue;
}
}
扩展方法:
public static DataTable ConvertToDataTable(this IEnumerable enumerable)
{
var dataTable = new DataTable();
foreach (PropertyDescriptor pd in TypeDescriptor.GetProperties(typeof(T))) {
dataTable.Columns.Add(pd.Name, pd.PropertyType);
}
foreach (T item in enumerable) {
var Row = dataTable.NewRow();
foreach (PropertyDescriptor dp in TypeDescriptor.GetProperties(typeof(T))) {
Row[pd.Name] = pd.GetValue(item);
}
dataTable.Rows.Add(Row);
}
return dataTable;
}
传统写法:
DataTable table = new DataTable();
table.Columns.Add("UserID", typeof (long));
table.Columns.Add("UserName", typeof (string));
IEnumerable<T> database = //获取数据源;
foreach (T info in database) {
DataRow row = dt.NewRow();
row["ID"] = database.ID;
row["Name"] = database.Name;
table.Rows.Add(row);
}
}
return table;
ObjectQuery
ToTraceString()
DECLARE @pageSize INT ;
DECLARE @pageIndex INT ;
SET @pageSize = 5
SET @pageIndex =2 ;
--第二页,每页显示5条数据
SELECT *
FROM ( SELECT ROW_NUMBER() OVER ( ORDER BY CreateDate ASC ) AS 'rowNumber' ,
*
FROM dbo.Task_Info
) AS temp
WHERE rowNumber BETWEEN ( ( ( @pageIndex - 1 ) * @pageSize ) + 1 )
AND ( @pageIndex * @pageSize )
public static string ToTraceString<T>(this IQueryable<T> query)
{
return ((System.Data.Objects.ObjectQuery) query).ToTraceString();
}
//调用
query.ToTraceString();