C# 获取包含where子句的集合作为Linq中的集合
以下是我的服务方法:C# 获取包含where子句的集合作为Linq中的集合,c#,linq,where-clause,C#,Linq,Where Clause,以下是我的服务方法: public List<RelatedInvoiceData> GetRelatedInvoices(InvoiceSearch invoiceSearchFilters) { List<InvoiceInfoView> invoices = _wiseStepDbContext.InvoiceInfoView.Where(i => i.RecruiterCompanyId == _securityManager.CurrentRec
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;
}
以前我在linq中使用where来表示单个客户Id,现在我希望过滤器具有多个CustomerID和多个VendorID
现在我想使用customerid数组。如何在Where子句中为数组编写LINQ。感谢您的帮助如果我理解正确,您的意思是
I.CustomerId
现在是一个数组或列表
。如果是这种情况,则可以使用.Contains()
方法。类似这样的操作应该满足您的需要:relatedInvoiceViewCollection=relatedInvoiceViewCollection.Where(i=>i.CustomerId.Contains(invoiceSearchFilters.CustomerId)).ToList()代码>
编辑:如果您想在两个数组中检查,这可能会有所帮助,您可以这样做:relatedInvoiceViewCollection=relatedInvoiceViewCollection.Where(i=>i.CustomerId.Intersect(invoiceSearchFilters.CustomerId.Any()).ToList()代码>
或
排序取决于您使用的LINQ变体(例如,LINQ到对象、LINQ到实体等)。不同的变体支持不同的操作符和操作子集。我提出的问题可能是错误的,我已经更新了问题,并有明确的要求。检查问题,更新我在提出正确问题时的错误。更新问题请检查要求并更新ans。
public class InvoiceSearch
{
public int[] CustomerId { get; set; }
public int[] VendorId { get; set; }
}
relatedInvoiceViewCollection.Where(x => relatedInvoiceViewCollection.Contains(invoiceSearchFilters.CustomerId)).ToList();
relatedInvoiceViewCollection.Where(x => x.Contains(invoiceSearchFilters.CustomerId)).ToList();