Need to convert type ‘System.Collections.Generic.List<String>’ to ‘System.Collections.Generic.List TrackerLibrary.Models.MySpecificModel>

This is probably an age-old question but after a solid day of Googling I am yet to figure out the answer.
Ultimately, I have a drop-down that needs to be populated by a list based on one of my model classes. I have the corresponding data in a database and I am retrieving the data with a stored procedure. I can get the data out and into a List of type string > but it actually needs to go into a List of type MyParticularModel>. Below I have provided more details. Could anybody tell me what I am doing wrong?

Error message is “Cannot implicitly convert type‘ System.Collections.Generic.List String> ’ to ‘System.Collections.Generic.List TrackerLibrary.Models.PersonModel> ’”

The second to last line of code is where I am getting the error message:

public List<PersonModel> GetPerson_All()

    {

        //spPeople_GetAll

        List<PersonModel> output;

        List<string> mylist;



        using (SqlConnection connection = new SqlConnection(GlobalConfig.CnnString(db)))

        {

            connection.Open();               

            using(SqlCommand cmd = new SqlCommand("dbo.spPeople_GetAll", connection))

            {

               

                cmd.CommandType = CommandType.StoredProcedure;



                DataSet dataset = new DataSet();

                SqlDataAdapter da = new SqlDataAdapter(cmd);

                da.Fill(dataset);



                output = (from r in dataset.Tables["dbo.People"].AsEnumerable() select r.Field<int>("id") + r.Field<string>("FirstName") + r.Field<string>("LastName") + r.Field<string>("EmailAddress") + r.Field<string>("CellPhoneNumber")).ToList();



            }

        }

        return output;

    }

Here is what the model looks like:

namespace TrackerLibrary.Models

{

  public class PersonModel

   {
     public int Id { get; set; }

     public string FirstName { get; set; }

     public string LastName { get; set; }

     public string EmailAddress { get; set; }

     public string CellphoneNumber { get; set; }


      public string FullName

      {

          get

          {

              return $"{ FirstName } { LastName }";

          }

      }

    }

}

Here is what the stored procedure looks like:

 USE [testorama2]

GO

SET ANSI_NULLS ON

GO

SET QUOTED_IDENTIFIER ON

GO

ALTER PROCEDURE [dbo].[spPeople_GetAll]

AS

BEGIN

   SET NOCOUNT ON;

  select * from dbo.People

END

I’m not a Java expert, but it looks like you are selecting the right information from the db but in string format resulting in an List of strings in the output variable.
Since your class is defined to return a List of type PersonModel, then for you should return a List of that type in the output variable.

So you need to change this line:

output = (from r in dataset.Tables["dbo.People"].AsEnumerable() select r.Field<int>("id") + r.Field<string>("FirstName") + r.Field<string>("LastName") + r.Field<string>("EmailAddress") + r.Field<string>("CellPhoneNumber")).ToList();

And create a class instance of PersonModel for each row in the query result before turning into a List.

Again not sure how you would do that in java but I hope that helps!

1 Like

Thank you for responding. This is actually for C# - ADO.Net. Digging more into this problem, its looking like the answer is going to be iterate over the data reader (once a data reader has been retrieved) and create PersonModel class objects for each row returned by the data reader.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.