如何获取条件为字符串数组的记录

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

在下面的代码中,profile 是一个字符串。我想知道如何在 profile 是字符串数组或列表的情况下编写查询。即数组或列表可以有多个元素,如 { 'Profile1','profile2'}

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

您可以使用高效的连接:

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

另一个效率较低的选项是 Contains:

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

简单地问,如果profile包含那个ProfileId

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