简体   繁体   中英

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;
    }

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)

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);
}

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);

PS: Also the line

    results = 0;

should probably look like:

    results = searchResult.Count;

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM