LINQ查询语法

编程入门 行业动态 更新时间:2024-10-09 20:21:19
本文介绍了LINQ查询语法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有两个不同的列表,其中一个是像List<int> idsList中一样的一堆ID,但是另一个是像List<MyObject> myObjectList这样的对象列表,其中的对象看起来像这样:

I have two different lists where one is a bunch of ID's as in a List<int> idsList, the other however is a list of objects like List<MyObject> myObjectList where the object looks like this:

class MyObject{ private List<int> ids; public MyObject(List<int> ids){ this.ids = ids; } public List<int> Ids{ get{ return ids; } } }

如您所见,每个对象可以包含一个或多个ID(永远不能为零或null).因此,我最后需要知道myObjectList中的哪些对象具有来自idsList的任何ID. 到目前为止,如果我这样做:

As you can see each object can contain one or multiple IDs (never zero or null ids). So what I need at the end is to know what objects in myObjectList have any id(s) from my idsList. So far if I do:

var ids = from g in onScreen where g.Ids.Contains(myIntVariable) select g;

它将给我包含myIntVariable的对象的列表.我不知道该怎么做,就是将idsList的内容与MyObject中的列表进行匹配. 谢谢!

it would give me a list of the object(s) that contain myIntVariable. What I do not know how to do is to match the content of the idsList with the list in MyObject. Thanks!

推荐答案

一种方法:

var listOfMyObjectsContainingAnIdFromIdsList = myObjectList.Where(myObject => myObject.Ids.Any(id => idsList.Contains(id)));

更多推荐

LINQ查询语法

本文发布于:2023-05-25 19:53:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/236216.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:语法   LINQ

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!