How do i convert a dynamic dictionary values to list?

10,867

Solution 1

Just cast it:

list.AddRange(((IEnumerable<string>)dic1.Values).ToList());

Also, you can go via dynamic again:

list.AddRange((dynamic)dic1.Values));

Solution 2

ToList<T>() is an extension method to IEnumerable<T>. But your dic1.Values is of dynamic type not IENumerable<string> so .Net can not find and use this extension method. You can try just simple foreach:

foreach (var value in dic1.Values)
  list.Add(value)
Share:
10,867

Related videos on Youtube

Rajesh
Author by

Rajesh

Certified IT professional with 16+ years commercial experience in designing/implementing Web/Window applications using Service Oriented (SOA)/Microservices architectures using various technologies A team player with people management skills, a proven track record for managing multiple projects with demanding deadlines and effective communication skills. Experience with SDLC process such as Agile, Spiral and Waterfall under TDD and BDD environments Expertise in ASP.NET 4.5/4.0/3.5, MVC, .NET Core, Winforms, WCF Services, RESTful services, C#. NET, VB.NET, NServiceBus, HTML, Client side scripting using JavaScript, JQuery and Ajax Hands on experience with VS.NET 2019, 2017, 2015 and in designing .NET Web Services, WCF/REST/WebAPI, Silverlight, ADO.NET, Entity Framework, SSRS, SSIS. Hands on with Amazon Web Services (AWS) - Route 53, CloudFormation, IAM, VPC, EC2, ELB, S3, Glacier, Lambda, SAM, API Gateway, CloudWatch, Auto Scaling, Kinesis, Inspector, Certificate Manager, Directory Services, SQS, DynamoDB, SNS, Cognito, STS, Systems Manager, Image Builder, AWS Organizations, SCP &amp; Billing Design/implement POC for converting existing API’s to be deployed as Dockers containers and managing using Kubernetes for high availability/scalability Design and implement Infrastructure as Code (IaC) in AWS using CloudFormation/Terraform Design and implemented Build Configuration/Pipelines as Code using Kotlin DSL for TeamCity as CI/CD tool Hands on with Alert Logic Web Application Firewall (WAF), Qualys Scan Proficient in using SQL Server 2019/2017/2014 and Oracle 9i databases and PL/SQL Experience with a range of version control systems like GIT, SVN, TFS,Clear Case, VSS Experience using tools like NUnit, Moq, Pure Coverage, FxCop, ANTZ profiler, LLBL Gen Pro, Entity Framework, N-Hibernate, Autofaq, Infragistics, Dev Express, Telerik Controls, Project Plan, Report builder, PowerShell, Resharper Active contribution on communities like Stack Overflow, MSDN. Possess strong analytical and problem solving skills Ability to apply the right technology to meet the business goals and provide cost-effective solutions Adaptive learner of new technology concepts, strong reverse engineering skill and good at trouble shooting problems Team player with a proven track record with sharp learning curve, quick learner of new technology concepts and good at trouble shooting problems

Updated on June 16, 2022

Comments

  • Rajesh
    Rajesh about 2 years

    I have the below code:

    List<Type> p1 = new List<Type>();
    p1.Add(typeof(int));
    p1.Add(typeof(string));
    dynamic genericDic = typeof(Dictionary<, >);
    dynamic specificDic1 = genericDic.MakeGenericType(p1.ToArray());
    dynamic dic1 = Activator.CreateInstance(specificDic1);
    dic1.Add(1, "John");
    dic1.Add(2, "Smith");
    dynamic genericLst = typeof(List<>);
    dynamic specificLst = genericLst.MakeGenericType(typeof(string));
    dynamic list = Activator.CreateInstance(specificLst);
    list.AddRange(dic1.Values.ToList());
    

    When i try to perform the list.AddRange(dic1.Values.ToList()); i get the following exception

    Public member 'ToList' on type 'ValueCollection' not found.

    Stack trace:

       at Microsoft.VisualBasic.CompilerServices.Symbols.Container.GetMembers(String& MemberName, Boolean ReportErrors)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.ObjectLateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
       at Microsoft.VisualBasic.CompilerServices.NewLateBinding.LateGet(Object Instance, Type Type, String MemberName, Object[] Arguments, String[] ArgumentNames, Type[] TypeArguments, Boolean[] CopyBack)
       at StyleResearch.DataManagement.DataStream.Business.MonthEndProcess.ConverToLegacyStructureInMemory(List`1 marketDataTypeIds, String tableName, Int32 placeHolderValue) in C:\Style Research\Work\SRDM\trunk\StyleResearch.DataManagement.DataStream.Business\MonthEndProcess.vb:line 1850
       at StyleResearch.DataManagement.DataStream.UI.DDLMonthEndProcess._Lambda$__6(LegacyStructureDetail legacy) in C:\Style Research\Work\SRDM\trunk\StyleResearch.DataManagement.DataStream.UI\DDLMonthEndProcess.vb:line 1198
       at System.Threading.Tasks.Parallel.<>c__DisplayClass2d`2.<ForEachWorker>b__23(Int32 i)
       at System.Threading.Tasks.Parallel.<>c__DisplayClassf`1.<ForWorker>b__c()
    

    NOTE: I have converted by VB.NET sample to C# but the stack trace is from the VB.NET project

    When i do the below it works absolutely fine:

    Dictionary<int, string> dic = new Dictionary<int, string>();
    dic.Add(1, "John");
    dic.Add(2, "Smith");
    List<string> lst = new List<string>();
    lst.AddRange(dic.Values.ToList());
    
  • Rajesh
    Rajesh almost 12 years
    Thanks for the reply, but I do know that approach.Keen to know why it fails when i create objects using reflection and not when doing it normally.
  • Nikolay
    Nikolay almost 12 years
    As i said that happens because of dynamic type and extension method (ToList). Extension methods are not supported for dynamics (stackoverflow.com/questions/5311465/…)
  • Rajesh
    Rajesh almost 12 years
    When i use the above code i get this exception: Public member 'Cast' on type 'ValueCollection' not found.
  • Dave Bish
    Dave Bish almost 12 years
    None of those work: 'System.Collections.Generic.Dictionary<int,string>.ValueColl‌​ection' does not contain a definition for 'Cast'
  • abatishchev
    abatishchev almost 12 years
    @Rajesh: Then try System.Linq.Enumerable.Cast<string>(input)
  • Dave Bish
    Dave Bish almost 12 years
    Also - this is worse than casting the whole collection to IEnumerable<string> - as it will attempt to cast each element in the collection to 'string' - not cast the whole collection to an Enumerable type.
  • Nikolay
    Nikolay almost 12 years
    As i understand the idea behind using dynamics and reflections is that you dont know type of elements in list at compile time (IEnumerable<string>) so you can not just cast to it at compile time. Overwise you can just use types list without reflection and dynamics
  • Dave Bish
    Dave Bish almost 12 years
    Agreed - but he's already declaring the type above - so, in this instance it's known.