在DocumentDb Query中的IN子句中传递字符串列表

时间:2016-03-21 11:18:03

标签: azure azure-cosmosdb

当我尝试使用IN和多个电子邮件ID来获取记录时,此查询有效:

var families = _client.CreateDocumentQuery<Restraunt>(_documentCollection.SelfLink,
"Select Restraunt.RestrauntId from Restraunt join Rest in Restraunt.Emails where Emails.Email IN ('abc@gmail.com','ab@gmail.com') ").AsEnumerable().ToList();

我想知道如何在查询中将电子邮件ID列表作为字符串传递? 我尝试直接传递字符串列表但无法解决。有没有办法做到这一点?

我们说我有

List<string> elist=new List<string>{"abc@gmail.com","b@gmail.com"}

如何在查询中传递elist?

2 个答案:

答案 0 :(得分:0)

您必须更改查询才能使用ARRAY_CONTAINS,例如:

Select Restraunt.RestrauntId 
from Restraunt 
join Rest in Restraunt.Emails 
where ARRAY_CONTAINS(['abc@gmail.com','ab@gmail.com'], Emails.Email)

然后您可以参数化该查询,并将电子邮件数组作为参数发送。

答案 1 :(得分:0)

你可以试试这个:

List<string> elist=new List<string>{"abc@gmail.com","b@gmail.com"};

string emails = string.Empty;

if (eList!= null)
{
    emails = "'" + string.Join("','", eList.Select(x => x !=  String.Empty? x :"").Distinct().ToArray()) + "'";
    emails = emails.Replace("','','", "','");
}

var families = _client.CreateDocumentQuery<Restraunt>(_documentCollection.SelfLink,
    "Select Restraunt.RestrauntId FROM Restraunt join Rest in Restraunt.Emails 
     WHERE Emails.Email IN (" + emails + ") ").AsEnumerable().ToList();