Prady
Prady

Reputation: 11310

How to get records with a where condition is a string array

In the code below, profile is a string. I would like to know how to write a query in case where profile is a string array or list. ie the array or list can have multiple elements like { 'Profile1','profile2'}

var ccData = (from p in db.ChannelContacts where p.ProfileId == profile select p);

Upvotes: 1

Views: 51

Answers (2)

Ondrej Janacek
Ondrej Janacek

Reputation: 12616

Simply ask, if profile contains that ProfileId

var ccData = (from p in db.ChannelContacts 
              where profile.Any(prof => prof == p.ProfileId) 
              select p);

Upvotes: 1

Tim Schmelter
Tim Schmelter

Reputation: 460108

You could use an efficient join:

var ccData = from p in db.ChannelContacts 
             join profileID in profiles // your collection
             on p.ProfileId equals profileID
             select p;

another less efficient option is Contains:

var ccData = from p in db.ChannelContacts 
             where profiles.Contains(p.ProfileId)
             select p;

Upvotes: 2

Related Questions