Reputation: 4888
After reading about 40-50 questions and answers (I have tried lots of things) that where all just slightly off the answer I still can't get my head around how this does not work:
IEnumerable<string> textSegs = from cd in cds
where cd.Artist.Equals("Dream Theater")
select cd.Artist;
foreach(string s in textSegs)
Console.Write("\nTrack: " + s);
//This outputs: 'Track: Dream Theater'
Now as for the other part:
IEnumerable<string> textSegs = from seg in myXMLDoc.Descendants("name")
where ((string)seg).Equals("Dream Theater")
select (string)seg;
//This puts: exactly what I need
Then I figured this would do the magic trick:
IEnumerable<string> textSegs = from seg in myXMLDoc.Descendants("name")
where ((string)seg).Equals(from cd in cds
where cd.Artist.Equals("Dream Theater")
select cd.Artist)
select (string)seg;
//This outputs: Everything that is inside the XMLDoc (no filter applied)
As for the format this code is in. I am afraid it has to be like this (assignment). I tried casting the sub query to a string but it tells me:
Cannot convert type 'IEnumerable<string>' to 'string'
Any help is appreciated!
Upvotes: 2
Views: 163
Reputation: 3810
your "from cd" in the right hand of the Equals is returning all results that match your criteria, not just one.
Upvotes: 1
Reputation: 782
Try a join, I can't think of a cleaner method to do it:
from seg in myXMLDoc.Descendants("name")
join cd in cds
on (string)seg equals cd.Artist
where cd.Artist.Equals("Dream Theater")
select (string)seg;
Haven't compiled, so it might have an error or two, but it's somewhere along these lines for sure :)
Upvotes: 1
Reputation: 149020
It sounds to me like you're trying to do this:
IEnumerable<string> textSegs =
from seg in myXMLDoc.Descendants("name")
where ((string)seg).Equals(
(from cd in cds
where cd.Artist.Equals("Dream Theater")
select cd.Artist).First())
select (string)seg;
Or this, which is a bit easier to read:
IEnumerable<string> textSegs =
from seg in myXMLDoc.Descendants("name")
let artist =
(from cd in cds
where cd.Artist.Equals("Dream Theater")
select cd.Artist).First()
where ((string)seg).Equals(artist)
select (string)seg;
Upvotes: 3
Reputation: 50855
You essentially need to ask if one set of data contains another subset of data:
var artistQuery = from cd in cds
where cd.Artist.Equals("Dream Theater")
select cd.Artist;
IEnumerable<string> textSegs = from seg in myXMLDoc.Descendants("name")
where artistQuery.Contains((string) seg)
select (string)seg;
I've broken out each query above to show the steps. You could also write it as one statement:
IEnumerable<string> textSegs = from seg in myXMLDoc.Descendants("name")
where (from cd in cds
where cd.Artist.Equals("Dream Theater")
select cd.Artist).Contains((string) seg)
select (string)seg;
Upvotes: 2