Yecats
Yecats

Reputation: 1815

Linq - Cannot implicitly convert type 'System.Collections.Generic.IEnumerable<System.Xml.Linq.XElement>' to 'System.Collections.Generic.List<string>'

I am getting the following error:

Cannot implicitly convert type 'System.Collections.Generic.IEnumerable' to 'System.Collections.Generic.List'. An explicit conversion exists (are you missing a cast?)

My code is as follows :

 public Profile PullActiveProfile()
  {
   //currentProfile.Decks = new List<string>();
    return currentProfile = (from profiles in xmlDoc.Element("PlayerPofiles").Elements("Player")where (string)profiles.Element("Active") == "True"
    select new Profile
      {
       Name = (string)profiles.Element("Name"),
       Type = (string)profiles.Element("Type"),
       Verified = (string)profiles.Element("Verified"),
       Password = (string)profiles.Element("Password"),
       Email = (string)profiles.Element("Email"),
       Sex = (string)profiles.Element("Sex"),
       Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
       Created = (DateTime)profiles.Element("Created"),
       Birthday = (string)profiles.Element("Birthday") ?? "",
       Wins = (string)profiles.Element("Ratio").Element("Win") ?? "0",
       Losses = (string)profiles.Element("Ratio").Element("Loss") ?? "0",
       Abandoned = (string)profiles.Element("Ratio").Element("Abandoned"),
       // The following line is where I get the error. The decks node can have many descendants
       Decks = profiles.Elements("Decks").Descendants() 
         .Select(x => x.ToString()).ToList();
       }).FirstOrDefault(); 
    }

Here is the node structure:

<PlayerPofiles>
<Player>
  <Name>Stacey - Online</Name>
  <Type>Full/Basic</Type>
  <Active>True</Active>
  <Verified>True</Verified>
  <Password>pass</Password>
  <Email>[email protected]</Email>
  <Sex>Female</Sex>
  <Avatar path="/images/Treasure/BroadSword.png" />
  <Ratio>
    <Win>0</Win>
    <Loss>0</Loss>
    <Abandoned>0</Abandoned>
  </Ratio>
  <Created>6/19/2011</Created>
  <Birthday>09/28/1989</Birthday>
  <Decks>
    <Base />
    <Booty />
  </Decks>
</Player>

Upvotes: 4

Views: 27478

Answers (3)

V4Vendetta
V4Vendetta

Reputation: 38230

I think you should be retrieving a List < string >, because just retrieving the Descendents will give you an [IEnumerable for XElement]. (http://msdn.microsoft.com/en-us/library/bb337483.aspx) But what you need is a an IEnumerable of type string. Replace 'requiredname' with what you need to fill up in the List:

profiles.Elements("Decks").Descendants()
              .Select(x => x.requiredname).ToList()

Upvotes: 8

Bala
Bala

Reputation: 256

Remove the ";" after ToList()

var profiles = xmlDoc.Element("PlayerPofiles").Elements("Player")
                     .where(profile =>(profile.Element("Active") == "True"))
                     .FirstOrDefault();

if(profiles!=null){
return new Profile
      {
       Name = (string)profiles.Element("Name"),
       Type = (string)profiles.Element("Type"),
       Verified = (string)profiles.Element("Verified"),
       Password = (string)profiles.Element("Password"),
       Email = (string)profiles.Element("Email"),
       Sex = (string)profiles.Element("Sex"),
       Avatar = (string)profiles.Element("Avatar").Attribute("path") ?? "",
       Created = (DateTime)profiles.Element("Created"),
       Birthday = (string)profiles.Element("Birthday") ?? "",
       Wins = (string)profiles.Element("Ratio").Element("Win") ?? "0",
       Losses = (string)profiles.Element("Ratio").Element("Loss") ?? "0",
       Abandoned = (string)profiles.Element("Ratio").Element("Abandoned"),
       // The following line removed the ;
       Decks = profiles.Elements("Decks").Descendants() 
         .Select(x => x.Value.ToString()).ToList()
       }); 
}
else
{
//Handle if null
}

Upvotes: 2

Pankaj Upadhyay
Pankaj Upadhyay

Reputation: 13594

What I would suggest you is to first fetch the record from xml and then do the mapping like following :-

var currentProfile = from profiles in xmlDoc.Element("PlayerPofiles").Elements("Player")
                     where (string)profiles.Element("Active") == "True".FirstOrDefault()
                     select profile
Profile profile = new Profile();
profile.Name = currentProfile.Name;
. // all your properties mapping
.
.
return profile;

Upvotes: 0

Related Questions