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#?

3
  • You want to know if MediaType is part of an string array? Is this corrrect? Commented Jun 18, 2013 at 7:23
  • this might give you a hint: stackoverflow.com/questions/2912476/… Commented Jun 18, 2013 at 7:26
  • isn't it just m => mediaGroups.Any(g => m.MediaType == g) Commented Jun 18, 2013 at 8:17

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

6 Comments

Jon, what is Or method here? Is it BitArray.Or ?
@SonerGönül: No, it's presumably a PredicateBuilder class which the OP is already using.
Hi, Im getting this error: "The specified linq expression contains refrences to queries with different contexts", I have also included the whole problem section.
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.
@xerxes: I think it would be better to start a new question about that, to be honest.
|
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

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.