Linq to 对象列表中的列表

发布于 2024-10-04 23:10:42 字数 629 浏览 0 评论 0原文

我有一个电影列表

List<Movie> MovieList

和一个选定类别的列表

List<string> SelCat

假设我想从电影列表中选择与 2 个类别匹配的位置,如下面的 SQL 语句:

SELECT * FROM MovieList WHERE MovieList.Category = 'Action ' AND MovieList.Category = 'Drama'

我可以像这样使用 linq 有点接近:

var q = (from b in MovieList where b.Categories.Any(p=> SelCat.Contains(p )) select b);

但它的作用类似于 OR 查询,而不是 AND。我希望它选择所有具有动作和戏剧类别的电影。

顺便说一句:Movie.Categories 是一个字符串列表。并且Movie.Categories 必须包含SelCat 中的项目

如何使用 Linq to Objects 实现此目的?

I have a list of movies

List<Movie> MovieList

and I have a list of selected categories

List<string> SelCat

And say I want to select from the movie list where it matches 2 categories, like the SQL statement below:

SELECT * FROM MovieList WHERE MovieList.Category = 'Action' AND MovieList.Category = 'Drama'

I can get kinda close with linq like so:

var q = (from b in MovieList where b.Categories.Any(p=> SelCat.Contains(p)) select b);

But it acts like an OR query, not an AND. I want it to select all movies that have a category of action and drama.

BTW: Movie.Categories is a List of string. AND Movie.Categories must contain items in the SelCat.

How do I achieve this with Linq to Objects?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

壹場煙雨 2024-10-11 23:10:42
var q = from m in MovieList where SelCat.All(c => m.Categories.Contains(c))

非常接近您用英语描述问题的内容:

选择电影类别包含 SelCat 中的所有类别的电影。

var q = from m in MovieList where SelCat.All(c => m.Categories.Contains(c))

Quite close to what you would say describing the problem in English:

Select movies where the movie categories contain all the categories in SelCat.

梦冥 2024-10-11 23:10:42
   var SelectedCategories = List<string>();//list of selected categories

from movie in MovieList
join selCat in Categories.Where(SelectedCategories.Contains(selCat.Category)
on movie.category equals selCat.category
select movie
   var SelectedCategories = List<string>();//list of selected categories

from movie in MovieList
join selCat in Categories.Where(SelectedCategories.Contains(selCat.Category)
on movie.category equals selCat.category
select movie
别再吹冷风 2024-10-11 23:10:42

如果您希望电影匹配所有的有趣类别(即SelCat中的所有类别都存在于movie.Categories中),您可以这样做:

MovieList.Where(movie => !SelCat.Except(movie.Categories).Any()); 

另一方面,如果您希望电影与至少 2 个所选类别相匹配:

MovieList.Where(movie => SelCat.Intersect(movie.Categories).Count() >= 2); 

If you want the movie to match all of the interesting categories (i.e. all of the categories in SelCat are present in movie.Categories), you can do:

MovieList.Where(movie => !SelCat.Except(movie.Categories).Any()); 

On the other hand, if you want the movie to match atleast 2 of the selected categories:

MovieList.Where(movie => SelCat.Intersect(movie.Categories).Count() >= 2); 
浅紫色的梦幻 2024-10-11 23:10:42
var result = from movie in movieList
             where selCat.All(selectedCategory => movie.Categories.Contains(selectedCategory))
             select movie;

请记住 .All().Any() 之间的区别

var result = from movie in movieList
             where selCat.All(selectedCategory => movie.Categories.Contains(selectedCategory))
             select movie;

remember the difference between .All() and .Any()

匿名的好友 2024-10-11 23:10:42

有点复杂:

var moviesInSelCat = MovieList.Where(m => SelCat.All(sc => m.Category.Any(c => c == sc)));

A bit convoluted:

var moviesInSelCat = MovieList.Where(m => SelCat.All(sc => m.Category.Any(c => c == sc)));
不一样的天空 2024-10-11 23:10:42

试试这个

var matches = MovieList.Where(m => SelCat.Except(
    m.Categories.Intersect(SelCat)).Count() == 0);

try this

var matches = MovieList.Where(m => SelCat.Except(
    m.Categories.Intersect(SelCat)).Count() == 0);
治碍 2024-10-11 23:10:42

只需先进行相交,然后进行例外即可。
它有效,很抱歉我必须用 VB 编写它。

Dim categories As New List(Of String)
Dim selected As New List(Of String)

        categories.Add("ali")
        categories.Add("ali2")
        categories.Add("ali3")
        categories.Add("ali4")

        selected.Add("ali2")
        selected.Add("ali4")


        Dim common = categories.Intersect(selected)

        If common.Except(selected).Count = 0 Then
            'true
        Else
            'false
        End If

just do an intersect followed by except.
it works, im sorry i had to write it in vb.

Dim categories As New List(Of String)
Dim selected As New List(Of String)

        categories.Add("ali")
        categories.Add("ali2")
        categories.Add("ali3")
        categories.Add("ali4")

        selected.Add("ali2")
        selected.Add("ali4")


        Dim common = categories.Intersect(selected)

        If common.Except(selected).Count = 0 Then
            'true
        Else
            'false
        End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文