简体   繁体   English

LINQ:比较两个列表

[英]LINQ: comparing two lists

I am using LINQ queries to compare two lists (genres & matching) to see if matching contains any Elements of genres .我正在使用 LINQ 查询来比较两个列表(流派和匹配),以查看matching是否包含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 .但是,在运行此代码时,我没有得到任何结果,因为where fs.FilmeSerienGenres.Any(fsg => genres.Any(g => g == fsg))返回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 . FgFsFgGenre属性都是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.我用两个Properties替换了gfsg的比较,还是不行,所以我觉得不是'=='和'.Equals()'的问题。

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 .无论如何,在我看来,您有两个表:一个FSList和一个FilmeSerienGenres序列。

There seems to be a one-to-many relation between FSList and FilmeSerienGenres: Every FS from the FSList has zero or FilmeSerienGenres. FSList 和 FilmeSerienGenres 之间似乎存在一对多的关系:FSList 中的每个FS都有零或 FilmeSerienGenres。 Every FilmeSerienGenre belongs to exactly one FS, namely the FS that the foreign key refers to.每一个 FilmeSerienGenre 都只属于一个 FS,即外键所指的 FS。

Every FilmeSerienGenres has a sequence of strings in g.FgGenreNavigation.GBez .每个 FilmeSerienGenres 在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.要求:给定一个字符串Txt ,从 FSList 中的所有 FS 给我,这些 FS 至少有一个 FilmeSerienGenre 至少有一个 Filmeg.FgGenreNavigation.GBez 等于 TXT。

Or in simple words: FSList is a sequence of Films and Series.或者简单地说:FSList 是一系列电影和系列。 Every one of them belongs to zero or more genres: Comedy, Action, Thriller, etc. You can see this in FS.FilmeSerienGenres.它们中的每一个都属于零个或多个类型:喜剧、动作、惊悚等。您可以在 FS.FilmeSerienGenres 中看到这一点。 You want those Films and Series that have at least one FilmeSerienGenre that matches Txt.您想要那些至少有一个与 Txt 匹配的 FilmeSerienGenre 的电影和系列。 I wouldn't be suprised that Filmeg.FgGenreNavigation.GBez is something like Thriller, Action, etc.我不会惊讶Filmeg.FgGenreNavigation.GBez类似于 Thriller、Action 等。

Your problem is, because every FS has a subList FilmeSerienGenres which each have a sublist genre.Filmeg.FgGenreNavigation.GBez.您的问题是,因为每个 FS 都有一个子列表 FilmeSerienGenres,每个子列表都有一个子列表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每当您有一个“子列表列表”并且想要将它们全部连接到一个大列表中时,请考虑使用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.换句话说:从 FSList 中的每个 fs,制作其所有 FilmeSerienGenres 中的所有 Filmeg.FgGenreNavigation.GBez 的大序列。 The result is a sequence of strings.结果是一个字符串序列。 If this big sequence has at least one value equal to txt , then keep the FS.如果这个大序列至少有一个值等于txt ,则保留 FS。 If txt is not in the sequence, don't keep the FS.如果 txt 不在序列中,则不要保留 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.如果它们是类, == 的默认实现将比较 class 哈希。 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.要么,要么你必须在这两种类型上实现 IEquatable<> ,然后使用.Equals() (而不是 ==),以便它调用你的接口方法。 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.您也可以直接覆盖 == ,但这应该始终是最后的手段,因为它不是类型安全的,并且可能会在未来打破其他开发人员的期望。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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