无法将类型无效转换为通用列表

时间:2014-04-24 09:09:11

标签: c# generics

我有两个列表:国家/地区列表和工作列表

public List<Countries> getSharedCountries(string brandName)
    {
        var items = SharedJobs.Where(a => a.BrandName == brandName);

        var items2 = items.OrderBy(a => a.CountryCode);

        Countries = new List<Countries>();

        string Country = null;

        foreach (var item in items2)
        {
            if (Country != item.CountryCode)
            {
                Country = item.CountryCode;

                Countries.Add(new Countries() { CountryCode = item.CountryCode, JobIDs = getSharedJob(item.CountryCode) });
            }
        }
        return Countries;
    }

    public void getSharedJob(string Country)
    {
        var items = SharedJobs.Where(a => a.CountryCode == Country);

        JobNetDetails = new List<JobDetail>();
        CareerBoardDetails = new List<JobDetail>();
        JobSharkDetails = new List<JobDetail>();
        JobServeDetails = new List<JobDetail>();

        int AusCount = 0;

        foreach (var item in items)
        {
            if (Country == "AUS")
            {
                AusCount++;

                if (AusCount % 4 == 0)
                {
                    JobNetDetails.Add(new JobDetail() { JobPageTitle = item.JobPageTitle, JobID = item.JobID, JobUrl = item.JobUrl });
                }
                else
                {
                    JobServeDetails.Add(new JobDetail() { JobPageTitle = item.JobPageTitle, JobID = item.JobID, JobUrl = item.JobUrl });
                }
            }
        }
    }

在我访问方法getSharedJob的行上,它出错并给我错误,不能隐式地将void转换为system.generic.List?

我很困惑为什么会这样?

3 个答案:

答案 0 :(得分:1)

由于您的方法的签名表明public void getSharedJob(string Country)它无效,所以它不会返回任何内容,您应该更改它并返回您希望的列表。

编辑:当我在评论中读到你需要返回4个列表时。 您有几种选择:

  • 您可以返回一系列列表;
  • 您可以返回列表列表;
  • 您可以返回自己的包含4个列表的类。

答案 1 :(得分:0)

尝试以下代码,该代码从您正在调用的方法返回jobDetails

 public List<Countries> getSharedCountries(string brandName)
            {
                var items = SharedJobs.Where(a => a.BrandName == brandName);

                var items2 = items.OrderBy(a => a.CountryCode);

                Countries = new List<Countries>();

                string Country = null;

                foreach (var item in items2)
                {
                    if (Country != item.CountryCode)
                    {
                        Country = item.CountryCode;
                        foreach (var jobDetail in getSharedJob(item.CountryCode))
                        {
                           Countries.Add(new Countries() { CountryCode = item.CountryCode, JobIDs = jobDetail.JobID });
                         }
                    }

                }
                return Countries;
            }

            public List<JobDetail> getSharedJob(string Country)
            {
                var items = SharedJobs.Where(a => a.CountryCode == Country);

                JobNetDetails = new List<JobDetail>();
                CareerBoardDetails = new List<JobDetail>();
                JobSharkDetails = new List<JobDetail>();
                JobServeDetails = new List<JobDetail>();

                int AusCount = 0;

                foreach (var item in items)
                {
                    if (Country == "AUS")
                    {
                        AusCount++;

                        if (AusCount % 4 == 0)
                        {
                            JobNetDetails.Add(new JobDetail() { JobPageTitle = item.JobPageTitle, JobID = item.JobID, JobUrl = item.JobUrl });
                        }
                        else
                        {
                            JobServeDetails.Add(new JobDetail() { JobPageTitle = item.JobPageTitle, JobID = item.JobID, JobUrl = item.JobUrl });
                        }
                    }
                }

              return JobServeDetails;
            }

答案 2 :(得分:0)

您的方法签名表示采用字符串变量并且不返回任何内容(void)。

public void getSharedJob(string country)

JobIDs期待值

JobIDs = getSharedJob(item.CountryCode)

所以你需要返回一个与JobIDs类型相匹配的值,我假设它是一个int列表或一个JobDetails列表。