2011-07-18 37 views
-1

我有下面的代碼不工作。這是說PartitionKey和RowKey的值無法解析。我希望有一些能夠給我一些建議的專家。到目前爲止,我所依據的是一些建議,但仍然無法正常工作。c中的泛型和謂詞#

public class BaseTable 
{ 
    public string PartitionKey { get; set; } 
    public string RowKey { get; set; } 
} 

public abstract class AzureTableBase<T> : IAzureTable<T> 
    { 

    // This method gives the errors. The idea of this method is that instead of 
    // supplying the predicate you can just supply a value for partitionkey and 
    // rowkey as parameters. The problem is that it doesn't work and give the 
    // following syntax errors: 
    // 
    // Cannot resolve symbol PartitionKey 
    // Cannot resolve symbol RowKey 
    public virtual T Get<T>(string pkey, string rkey) where T : BaseTable 
    { 
     var a = this.Query.Where(u => u.PartitionKey == pkey && u.RowKey == rkey); 
    } 

    // Following method works: 
    public virtual T Get(Expression<Func<T, bool>> predicate) 
    { 
     return this.Query.Where(predicate).FirstOrDefault(); 
    } 

    public virtual IQueryable<T> Query 
    { 
     get 
     { 
      TableServiceContext context = CreateContext(); 
      return context.CreateQuery<T>(TableName).AsTableServiceQuery(); 
     } 
    } 

} 

下面是我目前如何調用第二Get函數的例子:

SubTest = testTable.Get(u => u.PartitionKey == "XX" & u.RowKey == "YY") 

我要的是能夠調用它是這樣的:

SubTest = testTable.Get("XX","YY") 
+1

實際的錯誤信息會有幫助。沒有更多的信息很難說。我不知道'this.Query'的類型是什麼,因爲它似乎是導致問題的原因。 – asawyer

+0

什麼是「這個」?是「獲取」BaseTable的一種方法嗎? –

+0

將this.Query定義爲IEnumerable ? –

回答

0

這是要返回某種集合,可能是IQueryable

this.Query.Where(u => u.PartitionKey == pkey && u.RowKey == rkey); 

你的方法聲稱返回一個T類型的值,在這種情況下它被限制爲BaseTable(或可能是一個子類)。這兩種類型不兼容。

+0

更新了更多信息的問題 – MIMI

+0

如果'BaseTable'只是一個不能從其他任何東西繼承的類,那麼這不是一個書面工作的祈禱。 – Yuck

+0

這是在stackoverflow上的另一位成員的建議後添加的。他們建議,如果我把那個類與成員PartitionKey和RowKey放在一起,那麼我不會得到錯誤。我願意接受任何其他建議。還有人建議我在那裏放置TableServiceEntity類。無論哪種方式,它仍然沒有幫助停止語法錯誤。謝謝 – MIMI

2

如果這是您的確切代碼,您不能擁有自由浮動功能,您需要將Get<T>放在一個類中,最好有一個Query字段。

根據您更新的帖子和您完全拒絕發佈完整的代碼段,我只能假設您並不真正關心(甚至不理解)您的代碼的功能,只是它編譯完成。關於這一點,

public abstract class AzureTableBase<T> : IAzureTable<T> where T : BaseTable 
{ 
    // don't need to re-generalize this function, T is already inherited from your class 
    public virtual T Get(string pkey, string rkey) 
    { 
     var a = this.Query.Where(u => u.PartitionKey == pkey && u.RowKey == rkey); 
     return a.FirstOrDefault();  // again, only guessing. 
    } 

    public virtual IQueryable<T> Query 
    { 
     get 
     { 
      TableServiceContext context = CreateContext(); 
      return context.CreateQuery<T>(TableName).AsTableServiceQuery(); 
     } 
    } 
} 
+0

我更新了問題。謝謝。 – MIMI

+0

@MIMI,不,你沒有,還沒有提到什麼'Query'應該是。 – Blindy

+0

對不起。我更新了,但沒有直接更新你的問題。我剛剛添加了我認爲你所問的內容。謝謝 – MIMI

0

@Blindly是正確的,但有一個其它塊...請問T或通過context.CreateQuery<T>(TableName).AsTableServiceQuery();類型回報實際上是從BaseTable繼承?

class OneOfYourTables : BaseTable 
{ 

} 

如果沒有的話那就是你的問題。