简体   繁体   中英

LINQ: comparing two lists

I am using LINQ queries to compare two lists (genres & matching) to see if matching contains any Elements of genres .

The Query:

IEnumerable<FilmeSerienGenre> genres =
   from g in _db.FilmeSerienGenres
   where g.FgGenreNavigation.GBez.Contains(txt)
   select g;
                        
IEnumerable<FilmeSerien> matching =
   from fs in FSList
   where fs.FilmeSerienGenres
      .Any(fsg => genres
         .Any(g => g == fsg))
   select fs;

However, when running this code I do not get any results, because where fs.FilmeSerienGenres.Any(fsg => genres.Any(g => g == fsg)) returns false . What did I do wrong?

EDIT:

where fs.FilmeSerienGenres
   .Any(fsg => genres
      .Any(g => g.FgFs == fsg.FgFs && g.FgGenre == fsg.FgGenre))

Both of FgFs and FgGenre Properties are int .

I replaced the comparison between g and fsg with two Properties, and it still does not work, so I think it is not a '==' and '.Equals()' problem.

Normally if would be easier for us if you gave us the requirements. Now you give us code that doesn't work, and ask us to give you code that does work, without telling us what you want.

Anyway, it seems to me that you you have two tables: an FSList and a sequence of FilmeSerienGenres .

There seems to be a one-to-many relation between FSList and FilmeSerienGenres: Every FS from the FSList has zero or FilmeSerienGenres. Every FilmeSerienGenre belongs to exactly one FS, namely the FS that the foreign key refers to.

Every FilmeSerienGenres has a sequence of strings in g.FgGenreNavigation.GBez .

Requirement: Given a string Txt , give me from all FS in the FSList, those FS, that have at least one FilmeSerienGenre that has at least one Filmeg.FgGenreNavigation.GBez that equals TXT.

Or in simple words: FSList is a sequence of Films and Series. Every one of them belongs to zero or more genres: Comedy, Action, Thriller, etc. You can see this in FS.FilmeSerienGenres. You want those Films and Series that have at least one FilmeSerienGenre that matches Txt. I wouldn't be suprised that Filmeg.FgGenreNavigation.GBez is something like Thriller, Action, etc.

Your problem is, because every FS has a subList FilmeSerienGenres which each have a sublist genre.Filmeg.FgGenreNavigation.GBez.

Whenever you have a "list of sublists", and you want to concatenate them all into one big list, consider to use one of the overloads of Queryable.SelectMany

string txt = ...
var matchingFilmsAndSeries = FSList
    .Where(fs => fs.FilmeSerienGenres
         .SelectMany(genre => genre.Filmeg.FgGenreNavigation.GBez)
         .Contains(txt));

In words: from every fs in FSList, make one big sequence of all Filmeg.FgGenreNavigation.GBez that are in all its FilmeSerienGenres. The result is a sequence of strings. If this big sequence has at least one value equal to txt , then keep the FS. If txt is not in the sequence, don't keep the FS.

== definitely is your issue here. However, it isn't clear what you are comparing. If they are classes, the default implementation of == is going to compare the class hashes. Thus, they'd have to share the same instance of that data in both lists for that to work correctly.

I would, instead, focus on specific primitive properties that must equal both sides. Either that or you have to implement IEquatable<> on both types and then use.Equals() (instead of ==) in order for it to call your interface method. You could also override == directly, but that should always be a last resort as it isn't type safe and could break expectations of other devs in the future.

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