How to Convert a LINQ result to DATATABLE?

前端 未结 2 2080
情歌与酒
情歌与酒 2020-12-18 13:02

Is there any way to convert the result of a LINQ expression to a DataTable without stepping through each element?

2条回答
  •  自闭症患者
    2020-12-18 13:21

    Nope there is no way to create it without stepping through each element. The Linq expression is evaluated when needed so it will step through each row (for matching and selection).

    I think you should try using DataTable.Select() (MSDN link) method instead as it returns array of DataRow objects that you can add to new table as follows:

    var rows = [ORIGINAL DATA TABLE].Select("id>5");
    
    var dtb=[ORIGINAL DATA TABLE].Clone();
    
    foreach(DataRow r in rows)
    {
        var newRow = dtb.NewRow();
        newRow.ItemArray = r.ItemArray;
        dtb.Rows.Add(newRow);//I'm doubtful if you need to call this or not
    }
    

提交回复
热议问题