2011-05-09 84 views
0

我有一個傳遞一個或多個提供程序代碼的WebGet。對於每個供應商代碼,我需要爲其所在代碼存在的所有住院患者返回患者列表,作爲接受,出席或諮詢供應商。在WCF數據服務中構建LINQ查詢的Where子句

應該是:

WHERE (inpatient = 1) 
AND (dischargeddate IS NULL) 
AND ((attending = 'SMITH') OR (admitting = 'SMITH') OR (consulting = 'SMITH')) 

的 「for」 循環不附加條件的where子句。我究竟做錯了什麼??

感謝

[WebGet] 
    public IQueryable<vw_patient> GetInpatients(string providercode, string searchtext) 
    { 
     string[] providers = providercode.Split(new char[] { '|' }); 

     if (string.IsNullOrEmpty(searchtext)) 
     { 
      var results = (from p in this.CurrentDataSource.vw_patient 
          where p.inpatient.Equals(true) 
          && p.dischargedate.Equals(null) 
          select p); 

      foreach (string provider in providers) 
      { 
       results = results.Where<vw_patient>(e => e.attending.Equals(provider) || e.admitting.Equals(provider) || e.consulting.Contains(provider)); 
      } 

      results.OrderBy(p => p.roomloc).ThenByDescending(p => p.patientname); 
      return results; 
     } 
     else 
     { 
      var results = (from p in this.CurrentDataSource.vw_patient 
          where p.patientname.StartsWith(searchtext) 
          && p.inpatient.Equals(true) 
          && p.dischargedate.Equals(null) 
          select p); 

      foreach (string provider in providers) 
      { 
       results = results.Where<vw_patient>(e => e.attending.Equals(provider) || e.admitting.Equals(provider) || e.consulting.Contains(provider)); 
      } 

      results.OrderBy(p => p.roomloc).ThenByDescending(p => p.patientname); 
      return results; 
     } 
    } 

回答

0

我想你可能需要把查詢的結果爲臨時存儲變量(例如List<vw_patient>),然後追加一條到主列表。喜歡的東西: -

既然這樣,你是在循環的每次迭代覆蓋results變量

List<vw_patient> fullList = new List<vw_patient>(); 

foreach (string provider in providers){ 
    results = results.Where<vw_patient>(
     e => e.attending.Equals(provider) || 
      e.admitting.Equals(provider) || 
      e.consulting.Contains(provider) 
    ); 
    fullList.AddRange(result.ToList<vw_patient>()); 
} 
+0

謝謝鄧肯......這很好! 我假設這是返回結果的方式: return fulllist.AsQueryable (); – user680891 2011-05-11 12:49:39

+0

@ user680891:沒有probs。返回看起來也不錯。 – 2011-05-11 12:54:46

0

在地方的

foreach (string provider in providers) 
{ 
    results = results.Where<vw_patient>(e => e.attending.Equals(provider) || e.admitting.Equals(provider) || e.consulting.Contains(provider)); 
} 

你可以這樣做:

results.Where(e => providers.Contains(e.attending) || providers.Contains(e.admitting) || providers.Contains(e.admitting));