如何使用包含条件过滤包含整数数组的列表 [英] How to filter a list which contain an array of integer using contain condition

查看:61
本文介绍了如何使用包含条件过滤包含整数数组的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友



我需要过滤一个包含名为 paymentPortfolioIds 的属性的列表,这是一个整数数组

  public   int  [ ] paymentPortfolioIds { get ;  set ; } 





我需要用另一个int数组过滤这个列表



请不要两个 payementPortfolioIds和portfolioID是数组的



我尝试下面的代码给我一个错误

int []不包含contains的定义



如何解决此问题


编辑问题



我的班级结构

< pre lang =c#> public class 责任
{
public int liabilityId { get ; set ; }
public string name { get ; set ; }
public int [] paymentPortfolioIds { get ; set ; }
public int numYearsToPayment { get ; set ; }
public bool taxDeductible { get ; set ; }
}





我的要求是,我有返回List< liability>的方法。它接受一个输入参数,即整数数组

  public  静态列表<责任> liabilityreference( int  [] portfolioID)
{
List< Liability> lstlr = new List< Liability>();

lstlr = lstliablity
。其中(l = > l.paymentPortfolioIds.Contains( 1 )) // 这里我需要传递int数组
.ToList(小于责任>);

}





我的尝试:



  var  result = lstliablity 
.Where(l = > l.paymentPortfolioIds.Contains(portfolioID))
.ToList();

解决方案

您正在寻找这些数组的交集:

  var  result = lstliablity 
。其中(l = > l.paymentPortfolioIds.Intersect(portfolioID).Any())
.ToList();


嗯。

如果你说:

 payementPortfolioIds和portfolioID都是数组的



然后Contains是错误的,因为你要求它迭代一组int,试图比较集合的每个成员 - 即每个int值 - 对一个数组中的ts。

实际上,你要问的是:

  int  [ ] portfolioID; 
foreach int i in l.paymentPortfolioIds)
{
if (i == portfolioID)
{
...
}

这显然会导致我无法将数组转换为int错误。

不确定你到底想要做什么,但是这不会起作用!


将此添加到页面顶部



 使用 System.Linq; 


Hi Friends

I need to filter a list which contain a property named as paymentPortfolioIds which is an array of integer

public int[] paymentPortfolioIds { get; set; }



I need to filter this list with another int array

please not both payementPortfolioIds and portfolioID are array's

I tried below code which giving me an error
int[] does not contain a definition for contains

How to solve this

Question Edited

My class structure

public class Liability
    {       
        public int liabilityId { get; set; }      
        public string name { get; set; }       
        public int[] paymentPortfolioIds { get; set; }
        public int numYearsToPayment { get; set; }   
        public bool taxDeductible { get; set; }
    }    



My requirement is, i have method which return List<liability> which takes one input parameter which is array of integers

public static List<Liability> liabilityreference(int [] portfolioID)
{
  List<Liability> lstlr = new List<Liability>();
          
lstlr  = lstliablity
        .Where(l => l.paymentPortfolioIds.Contains(1))// here i need to pass int array
        .ToList(<Liability>);

}



What I have tried:

var result = lstliablity
            .Where(l => l.paymentPortfolioIds.Contains(portfolioID))
            .ToList();

解决方案

You're looking for the intersection of those arrays:

var result = lstliablity
            .Where(l => l.paymentPortfolioIds.Intersect(portfolioID).Any())
            .ToList();


Um.
If as you say:

both payementPortfolioIds and portfolioID are array's


then the Contains is wrong, because you are asking it to iterate through an array of ints, trying to compare each member of the collection - i.e. each int value - against an array of ints.
Effectively, what you are asking is:

int[] portfolioID;
foreach (int i in l.paymentPortfolioIds)
   {
   if (i == portfolioID)
      {
      ...
   }

Which is clearly going to cause a "I can't convert an array to a int" error.
Not sure exactly what you are trying to do here, but that is not going to work!


Add this using to the top of the page

using System.Linq;


这篇关于如何使用包含条件过滤包含整数数组的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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