根据不同类型的另一个列表中的项目查找一个列表中的项目

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了根据不同类型的另一个列表中的项目查找一个列表中的项目相关的知识,希望对你有一定的参考价值。

我有一个List<Class1>List<Class2>,我想从List<Class1>中存在项目ID的List<Class2>中获取所有项目,反之,则从List<Class1>中获得所有项目项目的ID 存在于List<Class2>中。

public class Class1

    public int Id  get; set; 
    public string AnotherId  get; set; 
    public decimal Price  get; set; 


public class Class2

    public int Id  get; set; 
    public int AnotherId  get; set; 
    public string Message  get; set; 

我用Contains尝试了几种解决方案,但找不到正确的方法。

答案

您可以尝试这样的事情:

另一答案

尝试这样的事情:

另一答案

有几种方法,最简单的是System.Linq方法WhereAllAny

以上是关于根据不同类型的另一个列表中的项目查找一个列表中的项目的主要内容,如果未能解决你的问题,请参考以下文章