2016-08-23 101 views
-4
List<DateTime> listdate = new List<DateTime>(); 

var InList = (from b in mstdocstats 
          join a in mstdocs on a.docid = b.docid 
          where a.vtype = 'table' 
          && a.DCREA.Month == "06" && a.DCREA.Year == "2016" 
          select a.docid).ToList(); 



var listreaddate = (from b in mstdocstats 
        join a in mstdocs on a.docid = b.docid 
        where InList.Contains(a.docid) && a.vtaid = '2' 
       && a.vtype = 'read' 
          select new 
           { 
            Read_DATE = ((from mstdoc in mstdocs 
                where 
                mstdoc.docid == a.docid && 
                mstdoc.vtype == "read" 
                select new 
                { 
                 mstdoc.DCREA 
                }).First().DCREA) 
           }).ToList(); 

,但我得到一個錯誤報告,C#語言不能轉換列表:無法隱式轉換類型 'System.Collections.Generic.List <AnonymousType#1>' 到 'System.Collections.Generic.List <System.DateTime的>'

"Error:Cannot implicitly convert type

'System.Collections.Generic.List<AnonymousType#1>' to 'System.Collections.Generic.List<System.DateTime>' 

請指引我正確的解決辦法,我怎麼能做到這一點,得益於前...

+1

首先,你不能在同一個方法中定義兩個同名的變量,如'List listdate'和'var listdate'。 –

+0

你有兩次定義相同的變量我的意思是listdate –

+0

好吧,我知道它的變量:) –

回答

0

以下行創建一個新的匿名對象,其中包含一個屬性,它是一個日期時間:

      select new 
          { 
           Read_DATE = ((from mstdoc in mstdocs 
               where 
               mstdoc.docid == a.docid && 
               mstdoc.vtype == "read" 
               select new 
               { 
                mstdoc.DCREA 
               }).First().DCREA) 
          } 

試試這個讓日期時間的列表:

      select ((from mstdoc in mstdocs 
               where 
               mstdoc.docid == a.docid && 
               mstdoc.vtype == "read" 
               select new 
               { 
                mstdoc.DCREA 
               }).First().DCREA) 
+0

其實嵌套的匿名對象也冗餘,可以刪除, –

+0

謝謝...你的答案解決了我的問題:) –

-1

您沒有選擇DateTime對象。最後一個查詢選擇具有單個屬性Read_DATE的新匿名對象,該對象實際上包含DateTime對象。你需要將其轉換爲DateTime,以分配給列出

+1

這是爲什麼downvoted? – Maarten

+0

如何轉換? –

+0

@ sebastian-schulz在他的回答中顯示了樣本。基本上,在你的代碼中,你可以替換last.ToList();與.Select(x => x.Read_DATE).ToList(); – dlxeon

相關問題