1

I am using a predicate builder class and I need to invoke the contains method on an array of strings so in the code below instead of radio I would be passing in an array of strings:

wherePredicate = wherePredicate.Or(m => m.MediaType.Contains("Radio"));

the full code section:

if (param.iMediaGroupID > 0)
{
    var wherePredicate = PredicateBuilder.False<MediaChannelModel>();

    var ss = new NeptuneRepository<Lookup_MediaTypes>();
    var mediagroups = ss.FindWhere(m => m.MediaGroupID == param.iMediaGroupID).Select(m => m.Name);
    //problem area
    wherePredicate = wherePredicate.Or(m => mediagroups.Contains(m.MediaType));
    predicate = predicate.And(wherePredicate);
}

mediaGroups is: ["Radio","Tv","Magazine"]

If m.MediaType is any of these values then the predicate is true.

Is there a way to do this in C#?

Ed Chapel
  • 6,842
  • 3
  • 30
  • 44
Farhad-Taran
  • 6,282
  • 15
  • 67
  • 121
  • You want to know if MediaType is part of an string array? Is this corrrect? – Jehof Jun 18 '13 at 07:23
  • this might give you a hint: http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array – Filip Jun 18 '13 at 07:26
  • isn't it just `m => mediaGroups.Any(g => m.MediaType == g)` – Jodrell Jun 18 '13 at 08:17

2 Answers2

7

I suspect you want something like:

wherePredicate = wherePredicate.Or(m => array.Contains(m.MediaType));

Or perhaps:

wherePredicate = wherePredicate.Or(m => array.Any(x => m.MediaType.Contains(x)));

If neither of those are what you're after, please clarify your requirements.

EDIT: The problem you're now facing is that you're not actually asking whether an array contains the value. You're asking whether a query contains a value. If you change it to an actual array, you may well find it works:

var mediagroups = ss.FindWhere(m => m.MediaGroupID == param.iMediaGroupID)
                    .Select(m => m.Name)
                    .ToArray();

However, if these are querying the same database, you'd be better off trying to do this in some kind of join.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Jon, what is Or method here? Is it `BitArray.Or` ? – Soner Gönül Jun 18 '13 at 07:26
  • @SonerGönül: No, it's presumably a `PredicateBuilder` class which the OP is already using. – Jon Skeet Jun 18 '13 at 07:33
  • Hi, Im getting this error: "The specified linq expression contains refrences to queries with different contexts", I have also included the whole problem section. – Farhad-Taran Jun 18 '13 at 07:41
  • Hi Jon, Could you help me convert the two contains methods to a Join, this currently causes a timeout exception because of the contains being too slow in EF5. I have included the table structures in the edited question. – Farhad-Taran Jul 04 '13 at 15:00
  • @xerxes: I think it would be better to start a new question about that, to be honest. – Jon Skeet Jul 04 '13 at 15:03
  • I did but noone has been able to help, could you have a look please, its here: http://stackoverflow.com/questions/17472773/how-to-use-a-join-in-linq-instead-of-contains?noredirect=1#comment25391752_17472773 – Farhad-Taran Jul 04 '13 at 15:06
0

Jon Skeet's answer worked perfectly for me. I had been struggling to make the .Contains search for a substring in a string array against the database, rather than try to find a substring in a single C# string object. Thank you!

Here's the modified code that worked for me:

var predicate = PredicateBuilder.False<ClientXMemberDetail>();
predicate = predicate.Or(x => strArrselectedCustomMemberNumbers.Any<string>(y => x.MemberID.Contains(y)));
CustomSearchMembersAlreadyMatched = ClientXContext.ClientXMemberDetails
                        .AsExpandable()
                            .Where(predicate)
                            .ToList()
                            .Select(r => r.MemberID.ToString()).ToList();

(ClientXContext above is an instance of the ObjectContext class, strArrselectedCustomMemberNumbers is a string array, ClientXMemberDetails is ObjectSet, where ClientXMemberDetail is the EntityObject)

Edit: Anonymized my client's name