开发者

How to convert a data table to a list of strongly typed objects in C# using LINQ?

I am doing a WPF Application written with C# code, My problem is I need to convert a data table from a dataset but sadly it throws me the exception "Cannot convert lambda exp开发者_开发知识库ression to type 'string' because it is not a delegate type" can you guys please help me with this.(I had the code which throws me the exception)

        SqlDataAdapter da = new SqlDataAdapter(commBuildingSelector);
        DataSet ds = new DataSet();
        da.Fill(ds);
        DataTable myTable = ds.Tables[0];
        List<Building> buildings = new List<Building>();
        buildings = (from bl in myTable
                    select new Building()
                    {
                        BuildingID = bl.BuildingID,
                        BuildingName = bl.BuildingName,
                        isActive = bl.isActive,
                        LastEditDate = bl.LastEditDate,
                        LastEditUser = bl.LastEditUser
                    }).ToList();
        return new List<Building>();


Use AsEnumerable and Field<T>:

buildings = (from bl in myTable.AsEnumerable()
             select new Building()
             {
                 BuildingID = bl.Field<int>("BuildingID")
                 // etc
             }).ToList();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜