John
John

Reputation: 285

Converting types C#

I am having difficulty with the following method:

public override List<Team> Search(Dictionary<string, string> prms,
                                    int pageSize, int page, out int results) 
{
    List<Team> t = null;
    //Team t = null;
    var tresults = new List<Team>();

    using (SqlConnection conn = DB.GetSqlConnection())
    {
        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = @"SearchForTeam";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            foreach (var key in prms.Keys)
            {
                cmd.Parameters.Add(key, prms[key]);
            }

            SqlDataReader reader 
                      = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (reader.Read())
            {
                var temp = Load(reader);

                if (t == null)
                {
                    t = temp;
                }
                else
                {
                    t.CityHistory.Add(temp.CityHistory[0]);
                }
            }
        }
    }
    results = 0;
    return t;
}

The error lies mainly with the if and else statement where the temp in the if block is claiming that it "cannot implicitly convert type DataLayer.Team to System.Collections.GenericList"

EDIT: Here is my load method:

        public Team Load(SqlDataReader reader)
    {
        var team = new Team()
        {
            TeamID = Int32.Parse(reader["TeamID"].ToString()),
            TeamName = reader["TeamName"].ToString()
        };

        team.CityHistory.Add(
            new TeamCity(
             Int32.Parse(reader["TeamCitiesID"].ToString()),
             team.TeamID,
             Int32.Parse(reader["CityID"].ToString()),
             reader["CityName"].ToString(),
             Int32.Parse(reader["YearStart"].ToString()),
             Int32.Parse(reader["YearEnd"].ToString())
            )
        );
    return team;
    }

Upvotes: 0

Views: 129

Answers (4)

DMAN
DMAN

Reputation: 471

You have to wrap temp into a List first to assign it to t:

if (t == null) {
  t = new List<Team>() { temp }
} else {
  t.add(temp);
}

Upvotes: 1

Eugene Podskal
Eugene Podskal

Reputation: 10401

VERY LONG COMMENT:

Consider the following refactoring of your method:

private IEnumerable<Team> LoadTeams()
{
    using (SqlConnection conn = DB.GetSqlConnection())
    {
        using (SqlCommand cmd = conn.CreateCommand())
        {
            cmd.CommandText = @"SearchForTeam";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;

            foreach (var key in prms.Keys)
            {
                cmd.Parameters.Add(key, prms[key]);
            }

            SqlDataReader reader 
                      = cmd.ExecuteReader(CommandBehavior.CloseConnection);

            while (reader.Read())
            {
                yield return Load(reader);
            }
        }
    }
}

public override List<Team> Search(Dictionary<string, string> prms,
                                    int pageSize, int page, out int results) 
{
    List<Team> searchResult = new List<Team>; 

    //Team t = null;
    var tresults = new List<Team>();

    foreach(Team team in LoadTeams())
    {
         if (team .....)
             searchResult.Add(team);
    }

    results = 0;

    return searchResult;
}

Take into account this NEEDED initialization:

    List<Team> searchResult = new List<Team>; 

And ask yourself: What should the following excerpt do? :

if (team .....)
   searchResult.Add(team);

P.S.: Also the line

    results = 0;

should probably look like:

    results = searchResult.Count;

Upvotes: 0

James Curran
James Curran

Reputation: 103485

t is defined as List<Team>, yet you later say t.CityHistory. CityHistory is clearly not a property of List<>. I'd guess it's a property of Team, but since you never show us that we can't say.

Show us the definition of Team and the method signature of Load() and perhaps we can give an answer.

UPDATE (from OP's update)

Now, I'm going to assume that you are getting multiple rows, one for each City, with the team info repeating. So, which you want is:

    var temp = Load(reader);
    // remove t definition above
    var  t = tresults.FirstOrDefault(team=> team.TeamId == temp.TeamId);


    if (t == null)
    {
        t = temp;
        tresults.Add(t);
    }
    else 
        t.CityHistory.Add(temp.CityHistory[0]);

(Updated again, based on Steve's comment)

Upvotes: 1

user2867342
user2867342

Reputation: 165

I'm having to guess a little here, but I assume that your Load() method is returning a data type 'DataLayer.Team', which sounds like its one Team, and you're trying to assign it to 't', which is a list of teams.

Try:

t.Add(temp) 

or

t.Add(temp as Team). 

It doesn't help that you're using 'var' declarations all the time.

Upvotes: -1

Related Questions