3

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 expression 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>();
1

1 Answer 1

4

Use AsEnumerable and Field<T>:

buildings = (from bl in myTable.AsEnumerable()
             select new Building()
             {
                 BuildingID = bl.Field<int>("BuildingID")
                 // etc
             }).ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

Gee men you're a heck of genius. thanks this really helps allot to me :)
@Allan Chua, you're welcome. Just as an FYI, if you want to mark an answer as accepted, you can click the checkmark outline next to it. As you ask more questions, a "% accepted" will show up under your name, and people will be more willing to answer your questions if you have a high % accepted number :)
can you help me again buddy i have a boolean value here that supposed to determine its value from a string(y/n) from the database. IsActive = (rt.Field<string>("IsActive") == "Y" || rt.Field<string>("IsActive") == "y" ? true : false) It always threw me false values can you help me please :)
use trim .. there must be some space.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.