简体   繁体   English

转换类型C#

[英]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" 错误主要在于if和else语句,其中if块中的温度声称其“无法将DataLayer.Team类型隐式转换为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 . t定义为List<Team> ,但稍后再说t.CityHistory CityHistory is clearly not a property of List<> . CityHistory显然不是List<>的属性。 I'd guess it's a property of Team , but since you never show us that we can't say. 我猜这是Team的财产,但是由于您从没有向我们表明我们不能说。

Show us the definition of Team and the method signature of Load() and perhaps we can give an answer. 向我们展示Team的定义和Load()的方法签名,也许我们可以给出答案。

UPDATE (from OP's update) UPDATE(来自OP的更新)

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 : 您必须先将temp包装到List中,然后将其分配给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 PS:也是线

    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. 我不得不在这里稍作猜测,但是我假设您的Load()方法返回的数据类型为“ DataLayer.Team”,听起来像是一个团队,而您正在尝试将其分配给“ t”,这是球队名单。

Try: 尝试:

t.Add(temp) 

or 要么

t.Add(temp as Team). 

It doesn't help that you're using 'var' declarations all the time. 一直使用'var'声明并没有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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