The following code produces the issue
Cannot implicitly convert type
'System.Collections.Generic.IEnumberable<DataField<string>>'
to 'DataFields'. An explicit conversion exists (are you missing a cast?).
How do I get around this? What am I doing wrong?
public class DataFields : List<DataField>
{
}
public abstract class DataField
{
public string Name { get; set; }
}
public class DataField<T> : DataField
{
public T Value { get; set; }
}
public static DataFields ConvertXML(XMLDocument data) {
DataFields result = (from d in XDocument.Parse(data.OuterXML).Elements()
select new DataField<string>
{
Name = d.Name.ToString(),
Value = d.Value
}).ToList();
return result;
}
Edited: Moving the information below to another question.
Using LINQ to create a List<T> where T : someClass<U>
In addition I would like to be able to do something like the following in this statement, in order to set the type of the value for each. How can I accomplish this.
select new DataField<[Attribute of element called type]>
{
Name = d.Name.ToString(),
Value = d.Value
}
As the error says you are trying to convert
DataField<T>
whereT
isstring
toDataFields
which inherits fromList
ofDatafield
and they are not same.So
DataField<string>
not equalsDataFields
, you could make yourDataFields
as a list ofstring
s if you wanted this to workDataFields : List<string>
How about this approach:
We know that each element is
DataField
so we could cast it to that type.Add the following constructor to the
DataFields
classThen
OK I figured out one way to handle this thanks to some insight from you guys