2014-08-30 170 views
0

我嘗試了幾乎所有的解決方案,但沒用。我無法獲得獨特的列表的特定列Priority.Any幫助非常感謝。與回報lstAlertNames發生不能隱式轉換類型'system.collections.generic.list <string>'到'system.collections.generic.list <entities.tablename>

public List<AlertType> GetAllAlertNames() 
     { 
      var lstAlertNames = _zyenaDbContext.AlertTypes.Select(x => x.Priority).Distinct().ToList(); 

      return lstAlertNames;   
     } 

以下錯誤:

cannot implicitly convert type 'system.collections.generic.list string ' 
to 'system.collections.generic.list zyenaEntities.AlertType 

在型號的AlertType

public string Priority { get; set; } 

回答

1

.Select(x => x.Priority)是隻選擇Priority屬性(typeof運算string),所以你選擇List<string>但您的方法正在返回List<AlertType>

不知道你真正想要的返回,但如果它是List<AlertType>morelinq有一個有用的擴展方法,它允許你做AlertTypes.DistinctBy(x => x.Priority);

+0

是的,就在這裏找到:http://stackoverflow.com/questions/4607485/linq-distinct-use-delegate-for-equality-comparer – firda 2014-08-30 09:09:29

+0

是@stepehen Muecke優先回報字符串。 DistinctBy在我的應用程序的實體框架中不受Intellisense支持。 – 2014-08-30 09:20:11

+0

但是如果我沒有錯的話,morelinq不會在實體上工作。它致力於LINQ到對象... – 2014-08-30 09:20:31

0

主要錯誤是我試圖只返回一個單一的財產優先實體的AlertType無法投射。

public List<string> GetDistinctAlertPriorities() 
     { 

      var lstAlertNames = _zyenaDbContext.AlertTypes.Select(x => x.Priority).Distinct().ToList(); 
      return lstAlertNames; 

     } 
相關問題