2

我有以下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'。存在显式转换(您是否缺少演员表?)”。

任何帮助将不胜感激!

4

1 回答 1

1

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

尝试这个

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

编辑:看起来您的 Books 属性类型错误。

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));
}
于 2010-02-09T19:10:11.843 回答