I have written a LINQ in C#
string etXML = File.ReadAllText("ET_Volume.xml");
string[] allLinesInAFile = etXML.Split('\n');
var possibleElements = from line in allLinesInAFile
                       where !this.IsNode(line)
                       select new { Node = line.Trim() };  
string[] xmlLines = possibleElements.ToArray<string>();
The problem is coming at last line, where the following errors arise:
System.Collections.Generic.IEnumerable<AnonymousType#1>does not contain a definition forToArrayand the best extension method overloadSystem.Linq.Enumerable.ToArray<TSource>(System.Collections.Generic.IEnumerable<TSource>)has some invalid argumentsInstance argument: cannot convert from
System.Collections.Generic.IEnumerable<AnonymousType#1>toSystem.Collections.Generic.IEnumerable<string>
What is wrong and what is the way to convert my var to a string[]?
You are creating an anonymous type here:
new { Node = line.Trim() }
That isn't necessary, just return
line.Trim()
and you have an IEnumerable of string. Then your ToArray will work:
var possibleElements = from line in allLinesInAFile
                       where !this.IsNode(line)
                       select line.Trim();  
string[] xmlLines = possibleElements.ToArray();
Another option is:
possibleElements.Select(x => x.Node).ToArray();
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With