2014-02-26 86 views
0

以下是我的LINQ查詢:LINQ子查詢問題

var varResourceStatusReportDataBase = 
    (
     from content in listContent 
     join workflowInstance in listWorkflkowInstance 
     on content.Field<string>("ows_ID").Trim() 
      equals workflowInstance.Field<string>("ows_Content ID").Split(';')[0].Trim() 
     join WorkflowInstanceStep in listWorkflowInstanceStep 
     on workflowInstance.Field<string>("ows_ID") 
      equals WorkflowInstanceStep.Field<string>("ows_Workflow Instance ID").Split(';')[0] 
     select new 
     { 
     ContentName = content.Field<string>("ows_Name"), 
     WorkflowInstanceId = workflowInstance.Field<string>("ows_ID"), 
     WIName = workflowInstance.Field<string>("ows_Title"), 
     WIPlannedStartDate = workflowInstance.Field<string>("ows_Planned Start Date") ?? "", 
     WIPlannedEndDate = workflowInstance.Field<string>("ows_Planned End Date") ?? "", 
     WIActualStartDate = workflowInstance.Field<string>("ows_Actual Start Date") ?? "", 
     WIActualEndDate = workflowInstance.Field<string>("ows_Actual End Date") ?? "", 
     WIApprovalDate = workflowInstance.Field<string>("ows_Approval Date") ?? "", 
     WITaskStatus = workflowInstance.Field<string>("ows_Status").ToUpper() ?? "", 
     WIMetadataStatus = workflowInstance.Field<string>("ows_Metadata Status") ?? "", 
     WIApprover = workflowInstance.Field<string>("ows_Approver").Replace("#", "").Split(';')[1].ToUpper() ?? "", 
     WISResponsible = WorkflowInstanceStep.Field<string>("ows_Responsible").Replace("#", "").Split(';')[1].ToUpper() ?? "", 
     WISDesiredEndDate= 
      (
      from WorkflowInstanceStep1 in listWorkflowInstanceStep 
      where (WorkflowInstanceStep1.Field<string>("ows_Status") =="IN PROGRESS" || 
        WorkflowInstanceStep1.Field<string>("ows_Status") =="ASSIGNED") && 
        workflowInstance.Field<string>("ows_ID") == WorkflowInstanceStep1.Field<string>("ows_Workflow Instance ID").Split(';')[0] 
      select new {abc = WorkflowInstanceStep1.Field<string>("ows_Desired End Date")}).Take(1) 
     }).Distinct(); 

我在上面查詢子查詢計算WISDesiredEndDate,但是當我執行這個查詢,我得到WISDesiredEndDate列沒有實際價值System.Linq.Enumerable+TakeIterator>d__3a'1[<>f__AnonymousType1'1[System.String]]值我想。 請給出同樣的建議。 謝謝。

回答

1

你想FirstOrDefault(),而不是Take(1)

由於從意見,你不想要一個匿名對象,以便改變選擇要

select WorkflowInstanceStep1.Field<string>("ows_Desired End Date")}).FirstOrDefault() 
+0

嗨,謝謝。它爲我工作,但現在「WISDesiredEndDate」列給價值{abc = 2014-02-28 00:00:00}但我只想要日期。 –

+0

我從我身邊做過。謝謝。 –

+0

@PrashantKhadatkar更新了答案 –

0

您可以我們let clause更可讀的代碼。不過,你必須通過調用FirstOrDefault方法強制執行linq表達式。

let desiredEndDate = (from WorkflowInstanceStep1 in listWorkflowInstanceStep .... Take(1)) 
select new 
{ 
... 
    WISDesiredEndDate= desiredEndDate.FirstOrDefault() 
}