Character literal must contain exactly one character -- 一天一點小知識

編程語言高度抽象化之後,錯誤也愈來愈讓人難以理解了,編程

NET編程最多見的一個錯誤, Object not set to the reference ,過了很久,才明白過來,編程語言

就是不明白爲啥微軟不說  "Null Object can not reference to its property"(空對象不能引用屬性)。spa

 

昨天碰到一個問題,其實之前也常常碰到,如今解決了,而且發現GOOGLE時候,沒有啥好東東能夠幫到各位一樣碰到問題的兄弟,對象

就想在這兒分享一下。it

相關技術: LINQ, DYNAMIC LINQ編譯

看如下代碼:變量

Dim query = From c In myDataContext.myTable

date

dim myEndDate as nullable(of datetime)=EndDateEdit.editValue循環

if myEndDate is not nothing then引用

  query = query.Where(DateFieldName & "<@0", cDate(myEndDate).AddDays(1))

end if

 

這樣的語句在編譯時候是能夠經過的,可是運行到第4句, query=query.Where(DateFieldName & "<@0", cDate(myEndDate).AddDays(1))

時就會報告錯誤:Character literal must contain exactly one character (字符文字必須包含一個字符) 

蒼天啊,大地啊,這個錯誤也太抽象了。

 

直接說個人解決方案

Dim query = From c In myDataContext.myTable

dim myEndDate as nullable(of datetime)=EndDateEdit.editValue

if myEndDate is not nothing then

  dim myEndDate1 as DateTime=cDate(myEndDate).addDays(1)

  query = query.Where(DateFieldName & "<@0", myEndDate1)

end if

 

萬事大吉。

 

分析緣由,LINQ屬於後期編譯,建議要給LINQ的變量,不要在LINQ表達式中進行運算,而應該先定義一個變量,運算事後,再在LINQ表達式中使用。

其實這個微軟在FOR 循環中使用LINQ時,有提醒到。

相關文章
相關標籤/搜索