1

as the question says I am trying to fill a table in the view from a data table. So far this is my code in the controller:

DataSet dsTemplates = new DataSet();

        string qryTemplets = "";
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
        conn.Open();
        SqlDataAdapter daTemplate = new SqlDataAdapter(qryTemplets, conn);
        daTemplate.Fill(dsTemplates, "FileInfo");
        DataTable dtTemplates = new DataTable();
        dtTemplates = dsTemplates.Tables[0];

        List<Models.GameModels.UserList> tables = dtTemplates.Rows.AsEnumerable()
       .Select(t => new Models.GameModels.UserList
         {
           Name = row["Name"],
           Record = row["Record"]
         })
             .ToList();

        conn.Close();
        return View(tables);

Its giving me the following exception:

'System.Data.DataRowCollection' does not contain a definition for 'AsEnumerable' and the best extension method overload 'System.Data.DataTableExtensions.AsEnumerable(System.Data.DataTable)' has some invalid arguments

Is this the best way to get render a table with multiple rows? Is there a workaround for my error? Thanks in advance. What else can I do?

1 Answer 1

2

This is the correct way to project the rows into your UserList object:

List<UserList> tables = dtTemplates.AsEnumerable()
    .Select(t => new UserList
    {
        Name = t["Name"],
        Record = t["Record"]
    }).ToList();
Sign up to request clarification or add additional context in comments.

Comments

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.