LINQ查询语法 [英] LINQ query syntax

查看:87
本文介绍了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查询语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆