【问题标题】:Windows Azure Mobile Service query tableWindows Azure 移动服务查询表
【发布时间】:2013-01-29 03:15:55
【问题描述】:

我使用 Windows Azure 移动服务。 我有一张元素表。 我要查询云数据库:

  • 选择 ID、名称 FROM Element ORDER BY creationTime

但我完全不了解 Windows Azure 移动服务的“查询”系统。 我有一个 IMobileServiceTable 但不知道如何处理它...

我查看了教程,他们解释了如何使用 Where 子句,但没有选择。而且我只需要选择一些列,因为我的元素有图片,我不想在我的 getAll 方法中下载它......

编辑:

我试试看:

Task.Factory.StartNew(() =>
{
    var query = table.Select(x =>
                new Element()
                {
                    Id = x.Id,
                    Name = x.Name,
                    Price = x.Price
                });
    var _items = query.ToListAsync().Result;
}).ContinueWith((x) => handleProductsArrived(x.Result));

但它不起作用。

【问题讨论】:

  • 您使用的是 .net 还是 javascript?

标签: c# .net xamarin azure-mobile-services


【解决方案1】:

您可以在 Carlos 那里找到一篇有用的帖子,其中包含相应的 SQL 查询:http://blogs.msdn.com/b/carlosfigueira/archive/2012/09/21/playing-with-the-query-object-in-read-operations-on-azure-mobile-services.aspx

例如:

function read(query, user, request) {
query.where({ UserId: user.userId })
     .select('id', 'MovieName', 'MovieRating')
     .orderBy('MovieName')
     .take(10);
request.execute();
}

会翻译成

选择 TOP 10 [id]、[MovieName]、[MovieRating] 来自 MovieRating WHERE Rating > 2 AND UserId = ? 按电影名称排序

所以对于你需要翻译的情况

选择 ID、名称 从元素 按创建时间排序

你会选择以下内容:

function read(query, user, request) {
    query.where({ UserId: user.userId })
        .select('id', 'Name', 'Element')
        .orderBy('creationTime')
    request.execute();
}

【讨论】:

【解决方案2】:

听起来你只是想用IMobileServiceTable做一个简单的查询

SELECT Id, Name FROM Element ORDER BY creationTime

如果你不介意使用IMobileServiceTable<TodoItem>,你可以试试:

1) 从对象中删除不需要的成员属性

例子:

public class TodoItem
{
    public int Id { get; set; }

    // REMOVE WHAT YOU DO NOT WANT
    //[DataMember(Name = "text")]
    //public string Text { get; set; }

    [DataMember(Name = "complete")]
    public bool Complete { get; set; }
}

2) 读取数据的代码如下:

private void RefreshTodoItems()
{ 
    items = todoTable
            .OrderBy( todoItem => todoItem.Id )
            .Take(10)
            .ToCollectionView();
    ListItems.ItemsSource = items;
}

基本上是:

SELECT TOP 10 Id, Complete FROM TodoTable ORDER BY Id

tod​​oTable 的代码示例位于http://www.windowsazure.com/en-us/develop/mobile/tutorials/get-started-wp8/

希望这会有所帮助。

【讨论】:

    【解决方案3】:

    如果您使用的是 .net,您几乎可以使用 linq。 查看示例应用程序 - 它在哪里 -

        private void RefreshTodoItems()
        {
            // This code refreshes the entries in the list view be querying the TodoItems table.
            // The query excludes completed TodoItems
            items = todoTable
                .Where(todoItem => todoItem.Complete == false)
                .ToCollectionView();
            ListItems.ItemsSource = items;
        }
    

    例如,如果您不想返回您可以在调用 .ToCollectionView() 之前添加的 Complete 标志

    .Select(item=>new {item.Id, item.Text})
    

    这将创建一个包含指定两个成员的匿名类型(可以是具体类型)新对象的列表。

    【讨论】:

    • 我在第一篇文章中添加了一些细节。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-23
    • 2017-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多