-2

Here is my service method:

 public List<RelatedInvoiceData> GetRelatedInvoices(InvoiceSearch invoiceSearchFilters)
 {
   List<InvoiceInfoView> invoices = _wiseStepDbContext.InvoiceInfoView.Where(i => i.RecruiterCompanyId == _securityManager.CurrentRecruiterCompanyId).ToList();

   List<RelatedInvoiceData> relatedInvoiceViewCollection = GetRelatedInvoiceCollection(invoices);

   if (invoiceSearchFilters.CustomerId > 0)
   {
        relatedInvoiceViewCollection = relatedInvoiceViewCollection.Where(i => i.CustomerId == invoiceSearchFilters.CustomerId).ToList();
   }

   if (invoiceSearchFilters.VendorId > 0)
   {
        relatedInvoiceViewCollection = relatedInvoiceViewCollection.Where(i => i.VendorId == invoiceSearchFilters.VendorId).ToList();
   }

   return relatedInvoiceViewCollection;
}

here is my filterObject :

public class InvoiceSearch
    {
        public int[] CustomerId { get; set; }

        public int[] VendorId { get; set; }
    }

Previously I used where in linq for single customer Id now i want filter with multiple customerIds and multiple VendorIds.

Now I want to go with array of CustomerIds. How to write LINQ for Array in Where clause. Thanks for any help

Jinna Balu
  • 6,747
  • 38
  • 47
  • Sort of depends on what variation of LINQ you're using (e.g. LINQ to Objects, LINQ to Entities, etc.). Different variations support a different subset of operators and operations. – CodingGorilla Mar 28 '16 at 17:09

2 Answers2

2

If I understand correctly, you mean that i.CustomerId is now an array or List<>. If that's the case, then you can use the.Contains() method. Something like this should do what you want: relatedInvoiceViewCollection = relatedInvoiceViewCollection.Where(i => i.CustomerId.Contains(invoiceSearchFilters.CustomerId)).ToList();

Edit: This question may be helpful if you want to check for intersections in two arrays, which you can do in your case like this:relatedInvoiceViewCollection = relatedInvoiceViewCollection.Where(i => i.CustomerId.Intersect(invoiceSearchFilters.CustomerId).Any()).ToList();

Community
  • 1
  • 1
senschen
  • 794
  • 10
  • 27
  • I may be wrong in giving the questions, I have updated the question with clear requirement. check with the question update the Ans – Jinna Balu Apr 01 '16 at 07:19
0
relatedInvoiceViewCollection.Where(x => relatedInvoiceViewCollection.Contains(invoiceSearchFilters.CustomerId)).ToList(); 

or

relatedInvoiceViewCollection.Where(x => x.Contains(invoiceSearchFilters.CustomerId)).ToList(); 
Eminem
  • 7,206
  • 15
  • 53
  • 95
  • my mistake in asking the right question. Updated the question please check with the requirement and update the ans. – Jinna Balu Apr 01 '16 at 07:20