невозможно преобразовать из System.Collections.Generic.Listв System.Collections.Generic.IEnumerable - PullRequest
0 голосов
/ 30 ноября 2018

У меня есть следующий код внутри моего консольного приложения c #, где я вызываю метод с именем getInfo параллельно, используя метод WhenAll(), как показано ниже: -

class Program
    {
        static int concurrentrequests = int.Parse(ConfigurationManager.AppSettings["ConcurrentRequests"]);
        static SemaphoreSlim throttler = new SemaphoreSlim(initialCount: concurrentrequests);

        private static ScanInfo getInfo(string website)
        {
            throttler.Wait();
            ScanInfo si = new ScanInfo();
            int counter = 1;
            try
            {
              //code goes here..

            }
            catch (Exception e)
            {
              //code goes here
            }

            finally
            {

             throttler.Release();
            }

            }
            return si;
        }

        static void Main(string[] args)
        {
            Marketing ipfd = new Marketing();
            try
            {
                using (WebClient wc = new WebClient()) // call the PM API to get the account id 
                {
                   //code goes here
                }
            }
            catch (Exception e)
            {


            }
            var tasks = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
            var results = Task.WhenAll(tasks);

           //code goes here..   
        }       
    }

, но я получаю этоисключение: -

Argument 1: cannot convert from
System.Collections.Generic.List<Sales.ScanInfo> to
System.Collections.Generic.IEnumerable<System.Threading.Tasks.Task>

поэтому кто-нибудь может посоветовать, почему я получаю эту ошибку?

1 Ответ

0 голосов
/ 30 ноября 2018
  var tasks = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
  var results = Task.WhenAll(tasks);

заданий содержит результаты.Вам вообще не нужно ждать.Что заставляет тебя думать, что ты делаешь.Так что просто сделай

  var results  = ipfd.companies.Select(c => getInfo(c.properties.website.value)).ToList();
...