Kenzo_Gilead
Kenzo_Gilead

Reputation: 2439

Checking if a list is included in a property which is a string?

SCENARIO I have a class with a different number of attributes. One of them is a string.

OBJECTIVE Check if a list of string is included in that attribute.

EXAMPLE The string attribute could be a description of a car, just like:

"Chevrolet 100 has 3000cc, using 250Cv. The 55/95 tires are included in the price. Just for 2 PAX... etc ..."

And the checking list could be:

"3000cc", "250Cv", "55/95 tires"

So my code would be:

         Class_A
int      id
int      price
string   definition

Class_A car = new Class_A()
{
    id = 1,
    price = 100000,
    definition = "Chevrolet 100 has 3000cc, using 250Cv. The 55/95 tires are 
    included in the price. Just for 2 PAX... etc ..."
}

List<string> checkingList = new List<string>();
checkingList.Add("3000cc");
checkingList.Add("250Cv");
checkingList.Add("55/95 tires");

As examples I tried:

bool sucess;
sucess = car.Select(p => p.definition.Contains(checkingList)).FirstorDefault();
sucess = car.Select(p => p.Where(o => o.definition.Contains(checkingList)).FirstorDefault();
sucess = car.Select(p => p.Where(o => o.definition.ForEach.Contains(checkingList)).FirstorDefault();

Thanks in advance mates.

Upvotes: 0

Views: 75

Answers (1)

CodingYoshi
CodingYoshi

Reputation: 26989

Assuming you have a List<Class_A> named cars, use the Any method to check if any item from the checkingList is found within the definition property:

cars.Select(p => checkingList.Any(y => p.definition.Contains(y)).FirstorDefault();

If you need to make sure each and every string within checkingList is in definition property, then use All instead of Any.

Upvotes: 1

Related Questions