如何在Azure表存储中通过partitionkey进行查询?

发布于 2024-08-21 00:20:48 字数 1225 浏览 2 评论 0原文

我有以下用于图书的 TableServiceContext 类,我想通过partitionKey 查询表 Books,请忽略以下事实: 我使用分区键作为书名,这只是为了我的学习目的,

   public class BookDataServiceContext: TableServiceContext
        {

            public BookDataServiceContext(string baseAddress, StorageCredentials credentials)
                : base(baseAddress, credentials)
            {
            }

            public IQueryable<Book> Books
            {
                get
                {
                    return this.CreateQuery<Book>("Books");
                }
            }

            public void AddMessage(string name, int value)
            {
                this.AddObject("Books", new Book {PartitionKey=name, BookName = name, BookValue = value});
                this.SaveChanges();
            }

            public Book GetBookByPartitionKey(Guid partitionKey)
            {
                var qResult = this.CreateQuery<Book>("Books");
    This doesnt work ---->           // qResult = qResult.Where(e => (e.PartitionKey.CompareTo(partitionKey)==0));

            } 
}

我得到一个“无法隐式地将类型‘System.Linq.IQueryable’转换为‘System.Data.Services.Client.DataServiceQuery’。显式转换存在(您是否缺少演员表?)”在最后一行。

任何帮助将不胜感激!

I have the following TableServiceContext class for books, I want to query the table Books by the partitionKey, please ignore the fact that
I'm using the partition key as the book name, this is just for my learning sakes

   public class BookDataServiceContext: TableServiceContext
        {

            public BookDataServiceContext(string baseAddress, StorageCredentials credentials)
                : base(baseAddress, credentials)
            {
            }

            public IQueryable<Book> Books
            {
                get
                {
                    return this.CreateQuery<Book>("Books");
                }
            }

            public void AddMessage(string name, int value)
            {
                this.AddObject("Books", new Book {PartitionKey=name, BookName = name, BookValue = value});
                this.SaveChanges();
            }

            public Book GetBookByPartitionKey(Guid partitionKey)
            {
                var qResult = this.CreateQuery<Book>("Books");
    This doesnt work ---->           // qResult = qResult.Where(e => (e.PartitionKey.CompareTo(partitionKey)==0));

            } 
}

I get a "Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Services.Client.DataServiceQuery'. An explicit conversion exists (are you missing a cast?)" on that last line.

Any help will be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

熟人话多 2024-08-28 00:20:48

qResult 已由编译器分配了一个类型。您试图将其重新分配给不同的类型,但这是不允许的。

试试这个

var someOtherName = qResult.Where(e => (e.PartitionKey.CompareTo(partitionKey)==0));

编辑:您的图书属性似乎类型错误。

public DataServiceQuery<Book> Books
{
    get
    {
        return this.CreateQuery<Book>("Books");
    }
}

public Book GetBookByPartitionKey(string partitionKey)
{
    var qResult = Books.Where(e => (e.PartitionKey.CompareTo(partitionKey)==0));
}

qResult is already assigned a type by the compiler. You're trying to re-assign it to a different type, but that isn't allowed.

Try this

var someOtherName = qResult.Where(e => (e.PartitionKey.CompareTo(partitionKey)==0));

Edit: It looks like your Books property is the wrong type.

public DataServiceQuery<Book> Books
{
    get
    {
        return this.CreateQuery<Book>("Books");
    }
}

public Book GetBookByPartitionKey(string partitionKey)
{
    var qResult = Books.Where(e => (e.PartitionKey.CompareTo(partitionKey)==0));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文