11

The best way that I can explain what I'm trying to do is by giving an example:

I have a string StackOverflow in my database and when a user types OAW I would like to return that string and any other word that contains those three characters in any order.

I've tried playing with various LINQ/Lambda expressions but to no avail.

query.Where(a => a.SerialNumber.Contains(a));

I found a post here on SO that looks similar to mine but it's in Java.

I feel what I'm trying to do is extremely simple to implement but I'm just missing it. Any help will be greatly appreciated.

HiTech
  • 913
  • 1
  • 16
  • 34

4 Answers4

6

You could try something like this:

query.Where(str => value.All(str.SerialNumber.Contains));

Here for any word in your database, you check if all the characters of the value, OAW, -Using the All extension method- are contained in the str.SerialNumber.

Update

This

str.SerialNumber.Contains

is equivalent to this lambda expression:

x => str.SerialNumber.Contains(x)

The x refers to the random element of the sequence in which we apply the All. In other words to the random character of value.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • So wasteful to call `ToCharArray`. A string is already `IEnumerable`, so you can do LINQ on it right away. This then turns into Nathan's solution. – Jeppe Stig Nielsen Apr 22 '15 at 22:23
  • @JeppeStigNielsen you are completely correct. Thanks for you comment ! – Christos Apr 22 '15 at 23:07
  • @Christos Where are you getting 'a' from so you can do a.All() ? – HiTech Apr 23 '15 at 18:07
  • @Christos I was able to get your previous answer working. But i can't seem to figure out how to implement your edited answer. If you can maybe explain how to use your new answer a bit better i will accept this as my answer. – HiTech Apr 23 '15 at 19:08
  • @HiTech Hi, I had to correct my answer. Let me explain you what is going on. – Christos Apr 23 '15 at 19:14
1

You can use a ContainsAll type of function.

public static bool ContainsAllItems(IEnumerable<T> a, IEnumerable<T> b)
{
    return !b.Except(a).Any();
}

NOTE

The function was borrowed from here

Community
  • 1
  • 1
Dan Drews
  • 1,966
  • 17
  • 38
1

You can do that by just modifying your code a little bit:

public bool DoesContain(char[] identifiers, string containingString)
{
    return !identifiers.Except(containingString.ToCharArray()).Any();
}

or:

private char[] identifiers = { /* Your identifiers*/ };

public bool DoesContain(string containingString)
{
    return !identifiers.Except(containingString.ToCharArray()).Any();
}

Either of these methods will return true if the string passed contains any of the specified characters.

Note:

Solution from Cameron MacFarland on this question.

Community
  • 1
  • 1
CalebB
  • 597
  • 3
  • 17
0

Looks like you can make multiple comparisons and && them, or use regular expressions. I prefer the former, here's a terse example:

arrOfCharacters.All(a.SerialNumber.Contains)

although you may need to litter your code with ToUpperInvariant()

Nathan Cooper
  • 6,262
  • 4
  • 36
  • 75