Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<string>'

Blerta

I am getting following error

Cannot implicitly convert type
'System.Collections.Generic.List<AnonymousType#1>' to
'System.Collections.Generic.List<string>'

I tried reading similar questions on stack overflow but didn't find a solution. My code is as below

var head =     
    from key in doc.Descendants("Header").Descendants("Article")
    select new 
     {
       value = (key.Value == String.Empty ?
       from q in doc.Descendants("Header").Descendants("Article") select q.Value : from a in doc.Descendants("Header").Descendants("Article") 
      select a.Attribute("DefaultValue").Value)

    };
List<string> hsourceFields = head.ToList();

If the value of xml node is empty I am reading the default value specified for that xml node

<Header>      
<Article>News</Article>
<Article DefaultValue ="Sport"></Article>    
</Header>

I want to be able to return a List which I am not able to by getting the error.

enter image description here

Josh

Looks like your code was getting a List<AnonType{value = List<string>}> instead of a List<string>

I think you want something like this that will select the text from an article or if that is empty it will take the value of the DefaultValue attribute. Note this doesn't handle when there is no text and no attribute.

var head =     
    from key in doc.Descendants("Header").Descendants("Article")
    select      
      string.IsNullOrEmpty(key.Value) ?
          key.Attribute("DefaultValue").Value :
          key.Value;
List<string> hsourceFields = head.ToList();

Or a slightly abbreviated version that uses xpath and method chains

var hsourceFields = doc.XPathSelectElements("/Header/Article")
     .Select (x => string.IsNullOrEmpty(x.Value) ?
        x.Attribute("DefaultValue").Value :
        x.Value).ToList() 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Cannot implicitly convert type to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

From Dev

Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

From Dev

Cannot implicitly convert type System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

From Dev

Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

From Dev

Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.Lis<AnonymousType#1>' to 'System.Collections.Generic.List<AdventureCycle.Models.Product>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

From Dev

Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<T>

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

From Dev

Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

From Dev

CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string DeprtmentName, decimal? TotalSalary>>'

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

From Dev

Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

From Dev

C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

From Dev

Cannot implicitly convert anonymous type to System.Collections.Generic.List

From Dev

Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

Related Related

  1. 1

    Cannot implicitly convert type to 'System.Collections.Generic.List

  2. 2

    Cannot implicitly convert type 'System.Collections.Generic.List

  3. 3

    Cannot implicitly convert type 'void' to 'System.Collections.Generic.List<string>'

  4. 4

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  5. 5

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List'

  6. 6

    Cannot implicitly convert type 'System.Collections.IList' to 'System.Collections.Generic.List

  7. 7

    Cannot implicitly convert type 'System.Collections.Generic.List<String>' to 'System.Collections.Generic.IEnumerable<turon.Model.Products_Products>

  8. 8

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

  9. 9

    Cannot implicitly convert type System.Collections.Generic.List

  10. 10

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1> in C#

  11. 11

    Cannot Implicitly Convert Type 'System.Collections.Generic.List<>' to 'IList<>'. An explicit conversion exists (are you missing a cast?)

  12. 12

    Cannot implicitly convert type System.Collections.Generic.List<IEnumerable> to <IEnumerable

  13. 13

    Cannot implicitly convert type 'System.Collections.Generic.List<MODEL#1>' to 'System.Collections.Generic.List<Model#2>

  14. 14

    Cannot implicitly convert type 'System.Collections.Generic.List<>' to 'System.Threading.Tasks.Task<>>

  15. 15

    Cannot implicitly convert type 'System.Collections.Generic.Lis<AnonymousType#1>' to 'System.Collections.Generic.List<AdventureCycle.Models.Product>'

  16. 16

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'string'

  17. 17

    Cannot implicitly convert type 'System.Collections.Generic.List<System.Data.DataRow>' to 'System.Collections.Generic.List<string>'

  18. 18

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List

  19. 19

    Cannot implicitly convert type 'System.Collections.Generic.List< >' to 'System.Collections.Generic.IList< >'

  20. 20

    Cannot implicitly convert type System.Collections.Generic.List<> to System.Collections.Generic.List<>

  21. 21

    Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<AnonymousType#1>' to 'System.Collections.Generic.IEnumerable<T>

  22. 22

    Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<FirstApp.Model.TeamDetails>

  23. 23

    Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Collections.Generic.List

  24. 24

    CS0029: Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type: string DeprtmentName, decimal? TotalSalary>>'

  25. 25

    Cannot implicitly convert type 'System.Collections.Generic.List' to 'System.Collections.Generic.List<Model.Room

  26. 26

    Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:>>' to 'System.Collections.Generic.List

  27. 27

    C# Cannot implicitly convert type 'System.Collections.Generic.List<AnonymousType#1>'

  28. 28

    Cannot implicitly convert anonymous type to System.Collections.Generic.List

  29. 29

    Web API Cannot implicitly convert type 'System.Collections.Generic.List<<anonymous type:

HotTag

Archive