Click here to Skip to main content
15,868,082 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends

I need to filter a list which contain a property named as paymentPortfolioIds which is an array of integer
C#
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
C#
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
C#
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:

C#
var result = lstliablity
            .Where(l => l.paymentPortfolioIds.Contains(portfolioID))
            .ToList();
Posted
Updated 17-Mar-16 1:03am
v3
Comments
Sascha Lefèvre 17-Mar-16 6:46am    
So is portfolioID an int-array or a single int (or something different)?
jinesh sam 17-Mar-16 6:50am    
portfolioID is an array. If its a single int it will work
Sascha Lefèvre 17-Mar-16 9:46am    
Did you take a look at my solution?
jinesh sam 17-Mar-16 12:58pm    
Yes its work for me. Thanks

You're looking for the intersection of those arrays:
C#
var result = lstliablity
            .Where(l => l.paymentPortfolioIds.Intersect(portfolioID).Any())
            .ToList();
 
Share this answer
 
Comments
jinesh sam 17-Mar-16 12:58pm    
Sascha its works for me..thanks a lot.
Sascha Lefèvre 17-Mar-16 13:01pm    
You're welcome! :)
BillWoodruff 17-Mar-16 15:59pm    
+5 Good catch !
Sascha Lefèvre 17-Mar-16 16:11pm    
Thanks, Bill!
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:
C#
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!
 
Share this answer
 
Add this using to the top of the page

C#
using System.Linq;
 
Share this answer
 
Comments
jinesh sam 17-Mar-16 7:04am    
It's already there

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900