2012-10-15 116 views
3

裏面我有以下的LINQ到實體查詢(使用odp.net)使用的ToString LINQ查詢select語句

 
Dim query = from Elemento in X.VIEW 
      where Elemento.code = "10" 
      Select New With {Elemento.code, .Time = Elemento.Time.ToString("HH:mm")} 
query.ToList() 

和代碼拋出的ToList()方法異常:

LINQ to Entities does not recognize the method 'System.String ToString()'

我已經閱讀過所有關於這個,但還沒有找到一個簡單的解決方法。


Lazyberezovsky有正確的答案,我不能讓它工作之前,因爲我這樣做:

 
Dim query = (from Elemento in X.VIEW 
      where Elemento.code = "10" 
      Select New With {Elemento.code, Elemento.Time}).ToList 
Dim query2 = from elemento in query 
     Select New With {elemento.code, TIME = elemento.Time.ToString("HH:mm")} 

Dim result = query2.ToList() 

但是,這並不工作,顯然你必須做一個一小步。

回答

6

您可以將DateTime轉換爲內存中的字符串。只是要ToList電話轉換時間之前:

在C#:

var query = from Elemento in X.VIEW 
      where Elemento.code == "10" 
      select new { Elemento.code, Elemento.Time }; 

var result = query.ToList() // now you are in-memory 
        .Select(x => new { x.code, Time = x.Time.ToString("HH:mm") }); 

在VB.Net:

Dim query = From Elemento In X.VIEW 
    Where Elemento.code = "10" 
    Select New With {Elemento.code, Elemento.Time} 

Dim result = query.ToList() _ 
    .Select(Function(x) New With {x.code, .Time = x.Time.ToString("HH:mm")}) 

BTW你爲什麼選擇Elemento.code到的結果,如果你被它的過濾操作員(總是等於「10」)。

+0

Elemento.Code結果只是爲了顯示我遇到的問題:P –

+0

我會稍微嘗試一下你的解決方案,並讓你知道,我想我已經嘗試了它,但它沒有工作,我會讓你知道,thxs –

+0

@ steve-konves感謝編輯! –