0

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:

  • Array["Ax", "By", "Cz", "Dw", "Eg"] contains Subarray["A", "By", "E"]
  • Array["Ax"] contains Subarray["x"]
  • Array["Ax", "By"] contains Subarray["Ax", "By"]

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.

2 Answers 2

3

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)));
Sign up to request clarification or add additional context in comments.

3 Comments

Yes that is exactly what I need but I've got little issue: var auctions = from o in db.auctions select o; Like this I'm getting my result from database my bigger array should be: each auction.productName.split(' '). I've got smaller array like string[] array but I've making bigger array from each auction result in var auctions?
@luka032 - didn't understand your question
I've edit my question so I hope I've made it more clear now.
0

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));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.