IKnowledge
IKnowledge

Reputation: 221

Get items which contain any of the strings in a Collection

What I have right now is this:

var dividendItemsRecord = yearItem.GetType().GetProperties().Where(x => x.Name.Contains("Record"));
var dividendItemsPayable = yearItem.GetType().GetProperties().Where(x => x.Name.Contains("Payable"));
var dividendItemsCash = yearItem.GetType().GetProperties().Where(x => x.Name.Contains("Cash"));

var dividendItems = dividendItemsRecord.Concat(dividendItemsPayable)
                                        .Concat(dividendItemsCash)
                                        .ToList();

This is clearly not the way I would like to do it, I am wondering if anybody knows a way to do it in one step.

PS: The items found will never intersect

Upvotes: 0

Views: 45

Answers (2)

mmg666
mmg666

Reputation: 353

The following should do it :

var listOfItems = yearItem.GetType().GetProperties().Where(x => x.Name.Contains("Record") || x => x.Name.Contains("Payable") || x => x.Name.Contains("Cash")).ToList();

Upvotes: 1

user3021830
user3021830

Reputation: 2924

You can join multiple conditions in one, with logical OR statement:

var dividend = yearItem.GetType().GetProperties().
Where(x => x.Name.Contains("Record") || x.Name.Contains("Payable") || x.Name.Contains("Cash"));

Upvotes: 2

Related Questions