尝试将来自LINQ查询的结果存储到ObservableCollection中,但linq的结果是十进制类型。将十进制的linq结果转换为ObservableCollection <string>
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.
我看着here但它并没有帮助我很多。
UPDATE
我曾尝试没有成功如下:
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.
感谢迈克....这工作完美! – Robert 2013-03-27 17:43:34