luka032
luka032

Reputation: 955

ASP .Net: Linq check if string array contains string subarray

Is there any way to check if one string array contains one string subarray? Elements in array doesn't have to be fully identical as elements in subarray, if it's not same strings it can only contain subarray's element as part of string in bigger array.

For example:

I need to write this in Linq syntax, I'm getting subarray as input "text" via form, and I'm splitting it with char(' '), on the other hand I'm getting bigger array as string split by char(' ') from database field.

What I want to do is to do "search" on client, to check if partially I can filter my database result.

I'm getting my subarray from client browser via input text field and that's OK, but I am creating Array from each result got from database:

var auctions = from o in db.auctions select o;

I need to pass auction.productName.split(' ') - that would be Array.

Need to filter auctions var by checking if each productName.split(' ') contains string[] words subarray.

Upvotes: 0

Views: 1740

Answers (2)

Zein Makki
Zein Makki

Reputation: 30062

You need to check if All elements in the Sub Array have at least on matching string in the original array where string of the Sub is a subset of the string in the original.

Case insensitive:

string[] array = new string[] { "Ax", "By", "Cz", "Dw", "Eg" };
string[] subArray = new string[] { "A", "By", "E" };

bool result =
subArray.All(sub => array.Any
        (item => item.IndexOf(sub, StringComparison.InvariantCultureIgnoreCase) >= 0));

Upvotes: 0

Gilad Green
Gilad Green

Reputation: 37281

If what you are asking is to check if: all the items of a sub-array are contained in a "parent" array then try this:

subArray.All(subItem => array.Any(item => item.Contains(subItem)));

Upvotes: 3

Related Questions