-1

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.

Naik
  • 25
  • 11

2 Answers2

1

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.

Harald Coppoolse
  • 28,834
  • 7
  • 67
  • 116
  • Thank you for this answer; however I am getting a "CS1929" compiler error 'IEnumerable' does not contain a definition for 'Contains' and the best extension method overload 'MemoryExtensions.Contains(ReadOnlySpan, string)' requires a receiver of type 'ReadOnlySpan' – Naik May 20 '21 at 21:54
  • This is a good example why you should give us your classes in your question. If you don't we have to guess, and apparently we sometimes guess wrong. On the other hand: if it says, that the sequence is a sequence of char, and you tried to use `Contains` with a string, haven't you got any clue how to solve this? – Harald Coppoolse May 21 '21 at 18:39
  • No, I do not have any clue how to solve this. I do not really get why I am getting this Error in the first place. – Naik May 21 '21 at 19:41
  • 1
    The error says that it does not know method `IEnumerable.Contains`. So you know it is in `Contains(txt)`. What is the input of `Contains(txt)`? To try out what the input is, type a dummy statement: `var x = FsList.Where(...).Selectmany(...)`. What does this expression mean? What do you expect to be the type? Is it really this type? In other words, instead of `var x =` can you type the real type `IEnumerable<...> x =`? Because you didn't tell GBez, I don't know. – Harald Coppoolse May 24 '21 at 15:13
0

== 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.

Daniel Lorenz
  • 4,178
  • 1
  • 32
  • 39
  • Well as you see in my edit, I changed the comparison between the two classes to (as suggested) a comparison between specific primitive properties; however I still get false, it is definitely not an == issue. – Naik May 20 '21 at 20:00
  • @Naik What is "FSList". How do you know there actually are any matching items here? – Daniel Lorenz May 20 '21 at 20:14
  • FSList is a List of the class 'FilmeSerien' and this class contains an ICollection. I know it because I put all the Data for testing purposes. – Naik May 20 '21 at 20:45