Home > Software design >  Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'Sy
Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'Sy

Time:04-04

I have the code below:
List<XElement> DBListSetxElements = DbListSetItems[0].Value.Root.Descendants("ListSet").Select(desc => desc.Attribute("Name")).Distinct().ToList();

While compile getting error Error CS0029 Cannot implicitly convert type 'System.Collections.Generic.List<string>' to 'System.Collections.Generic.List<System.Xml.Linq.XElement>'

Need Help.

CodePudding user response:

Using the toList() method you always get a List. But you want to store it in a list with XElements. Maybe a simple conversion with the help of a cast is enough. I don't think that works, though. What you can do is write your own method that converts your input to a List.

CodePudding user response:

Actually you need to provide the DbListSetItems class definition if you want any more help. Seems you're trying to select the name of the XML element and perhaps in the DbListSetItems data you have the content.

This solution creates a XML Element but the content is null As I said if you want to populate with some other DbListSetItems property you need to provide us some info (or get the apropiate value from it like desc.Attribute("theApropiateAtributeNameValue") ) instead null:

        List<XElement> DBListSetxElements = DbListSetItems[0].Value.Root.Descendants("ListSet")
            .Select(desc => new XElement(desc.Attribute("Name"), null)).Distinct().ToList();
  • Related