在Azure中移动服务WHERE子句把多个值子句、多个、中移动、Azure

2023-09-06 16:09:17 作者:我该如何去述说。

我想弄清楚如何将多个值插入,其中条款;这是什么样的东西,你会在SQL中使用子句。

I'm trying to figure out how to put multiple values into a WHERE clause; this is the kind of thing you'd use a IN clause for in SQL.

我目前的code:

if (Log.Count() == 1)
{
    items = itemTable
        .Where(Item => Item.id == Log[0].i_id)
        .ToCollectionView();
}
else if (Log.Count() == 2)
{
    items = itemTable
        .Where(Item => Item.id == Log[0].i_id || Item.id == Log[1].i_id)
        .ToCollectionView();
}
else if (Log.Count() == 3)
{
    items = itemTable
        .Where(Item => Item.id == Log[0].i_id || Item.id == Log[1].i_id || Item.id == Log[2].i_id)
        .ToCollectionView();
}

这是pretty的讨厌。我不能找到一种方法,把多个值成,其中条款没有一个大的if语句。任何想法?

It's pretty nasty. I can't find a way to put multiple values into that WHERE clause without a big if statement. Any ideas?

推荐答案

我面临着同样的问题。使用任何或包含如其他人所说给了我一个不支持异常。我贴在Azure移动服务论坛,被告知不支持呢。他们建议我用它完美地工作对我来说是扩展方法。下面是code我用。

I faced the same issue. Using Any or Contains as others have suggested gave me a Not Supported Exception. I posted on the Azure Mobile Services forum and was told that IN is not supported yet. They advised I use an extension method which worked perfectly for me. Below is the code I used.

 public static class Extensions
{
    public async static Task<List<T>> In<T>(this IMobileServiceTable<T> table, List<int> ids)
    {
        var query = new StringBuilder("$filter=(");
        for (int i = 0; i < ids.Count; i++)
        {
            query.AppendFormat("id eq {0}", ids[i]); //don't forget to url escape and 'quote' strings
            if (i < ids.Count - 1)
            {
                query.Append(" or ");
            }
        }
        query.Append(")");
        var list = await table.ReadAsync(query.ToString());
        var items = list.Select(i => MobileServiceTableSerializer.Deserialize<T>(i)).ToList();
        return items;
    }
}

您列表可​​以填充通过调用这样的扩展方法:

Your list can be populated by calling the extension method like this:

FavoriteJokesList = await jokeItemTable.In(favoriteJokeIds);

来源:http://social.msdn.microsoft.com/Forums/en-US/azuremobile/thread/b573ff6c-1f6b-4846-b44d-4678e3d26f66