关闭

Linq 学习

标签: LinqDatatable
606人阅读 评论(0) 收藏 举报
分类:
        DataTable dt=new DataTable();
        dt.Columns.Add("ID", typeof(int));
        dt.Columns.Add("Age", typeof(int));
        for (int i = 0; i < 10; i++)
        {
            DataRow dr = dt.NewRow();
            dr["ID"] = i + 1;
            dr["Age"] = DateTime.Now.ToString("fff");
            dt.Rows.Add(dr);
        }

        IEnumerable<DataRow> query1 = from contact in dt.AsEnumerable()
                                      where contact.Field<int>("ID") > 5
                                      select contact;

        DataTable contacts1 = query1.CopyToDataTable();
DataTable dtPosts = new DataTable();
using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["StackOverflow"].ConnectionString))
{
    conn.Open();
    using (SqlDataAdapter adapt = new SqlDataAdapter("SELECT TOP 100 Id, Title, Body, CreationDate FROM Posts WHERE Title IS NOT NULL ORDER BY Id", conn))
    {
        adapt.SelectCommand.CommandTimeout = 120;
        adapt.Fill(dtPosts);
    }
}

//use LINQ method syntax to pull the Title field from a DT into a string array...
string[] postSource = dtPosts
                    .AsEnumerable()
                    .Select<System.Data.DataRow, String>(x => x.Field<String>("Title"))
                    .ToArray();

var source = new AutoCompleteStringCollection();
source.AddRange(postSource);
textBox1.AutoCompleteCustomSource = source;
textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:35169次
    • 积分:525
    • 等级:
    • 排名:千里之外
    • 原创:16篇
    • 转载:15篇
    • 译文:0篇
    • 评论:10条
    文章分类
    最新评论