2011-05-27 263 views
-1

有返回視圖查詢在實體框架

var res = (from results in db.JobSearchAgents 
          where results.SiteID == 110 && results.UserID == sess 
          select results).AsEnumerable().Select(results => new Agentlist 
          { 
           JobSearchAgentID = results.JobSearchAgentID.ToString(), 

           EmailAddress = results.EmailAddress, 
           Keywords = results.Keywords, 


           Country = results.Country, 

           zipcode = results.ZipCode, 
           miles = results.Miles.ToString() 

          }); 

      return View(res); 
+6

你的問題是什麼? – 2011-05-27 12:41:01

回答

1

,你可以去內聯狀態

(from results in db.JobSearchAgents 
          where results.SiteID == 110 && results.UserID == sess 
          select results).AsEnumerable().Select(results => new Agentlist 
          { 
           //it is just an example 
           JobSearchAgentID =JobSearchAgentID!=null? results.JobSearchAgentID.ToString():[somthing else], 

           EmailAddress = results.EmailAddress, 
           Keywords = results.Keywords, 

           //another example 
           Country = String.IsNullOrEmpty(results.Country)?"No Country":results.Country, 
           //the last example 
           zipcode =results.Country=="United States"? "123": results.ZipCode, 
           miles = results.Miles.ToString() 

          }); 

      return View(res); 
+0

我必須檢查res.country ==「」那麼我能做什麼 – iProgrammer 2011-05-27 13:57:57

+0

你仍然可以使用內聯if。我已編輯我的答案 – 2011-05-27 14:02:35

+0

我必須檢查res.country ==「有限狀態」 – iProgrammer 2011-05-27 14:05:21

0

查詢可以改寫爲之前JobSearchAgentID如果條件適用於:

var res = from results in db.JobSearchAgents 
where results.SiteID == 110 && results.UserID == sess 
select new Agentlist 
{ 
    JobSearchAgentID = results.JobSearchAgentID.ToString(), 
    EmailAddress = results.EmailAddress, 
    Keywords = results.Keywords, 
    Country = results.Country, 
    zipcode = results.Country=="United States" ? "123" : results.ZipCode, 
    miles = results.Miles.ToString() 
}); 
string country = string.Emtpy; 

foreach (var agentLst in res) 
{ 
    country = agentLst.Country;  
} 

我最後的回答是在MVVM的上下文中,我們通常使用ObservableCollection(存在於System.Collections.Ob jectModel命名空間)。無論如何,我已經更新了我的答案。這將在你的情況下工作。 由於上面的linq查詢的結果是一個集合(IEnumerable),因此您需要遍歷它以獲取所需的數據。希望它能回答你的問題。

+0

它缺少程序集引用 – iProgrammer 2011-05-28 05:00:28

+0

它顯示類型或名稱空間可觀察集合無法找到 – iProgrammer 2011-05-28 05:12:35

+0

它顯示錯誤只有賦值,調用增量,遞減,新的對象表達式可以用作語句 – iProgrammer 2011-05-28 05:21:42