尝试将 LINQ 查询的结果存储到 ObservableCollection 中,但 linq 的结果是十进制类型。
ObservableCollection<string> cost =
new ObservableCollection<string>((from i in context.Items
where i.Cost != null
&& i.Cost > 0
orderby i.Cost
select i.Cost).Distinct());
它没有编译说'The best overloaded method match for 'System.Collections.ObjectModel.ObservableCollection<string>.ObservableCollection(System.Collections.Generic.IEnumerable<string>)' has some invalid arguments.
我看了这里,但对我没有多大帮助。
更新
我尝试了以下但没有成功:
ObservableCollection<string> cost =
new ObservableCollection<string>((from i in context.Items
where i.Cost != null
&& i.Cost > 0
orderby i.Cost
select i.Cost).Distinct()
.Select(i=>i.ToString()));
和
ObservableCollection<string> cost =
new ObservableCollection<string>((from i in context.Items
where i.Cost != null
&& i.Cost > 0
orderby i.Cost
select i.Cost.ToString()).Distinct());
当我在 LINQPad 中运行两者时,我收到以下错误:
LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.
Message LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression.