简体   繁体   中英

Why is Equals not working as expected

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!

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;

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;

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 :)

Equals右侧的“from cd”将返回符合条件的所有结果,而不仅仅是一个。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM