[英]convert linq results of decimal to ObservableCollection<string>
Trying to store results from a LINQ query into ObservableCollection but the results from linq are of decimal type. 尝试将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());
It doesn't compile saying 'The best overloaded method match for 'System.Collections.ObjectModel.ObservableCollection<string>.ObservableCollection(System.Collections.Generic.IEnumerable<string>)' has some invalid arguments.
它不会编译
'The best overloaded method match for 'System.Collections.ObjectModel.ObservableCollection<string>.ObservableCollection(System.Collections.Generic.IEnumerable<string>)' has some invalid arguments.
I looked here but it didn't help me much. 我在这里看了一下,但没有太大帮助。
UPDATE 更新
I have tried the following with no success: 我尝试了以下方法,但均未成功:
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()));
and 和
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());
When I run both in LINQPad, I get the following error: 当我同时在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.
Convert Cost
to a string with ToString
: 使用
ToString
将Cost
转换为字符串:
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());
Use whatever CultureInfo
you need, if any, when calling ToString()
. 调用
ToString()
时,请使用所需的CultureInfo
(如果有ToString()
。
Do the ToString
after the Distinct
. Distinct
之后做ToString
。 That way it's not creating so many strings and comparing those strings in the distinct. 这样,它就不会创建太多的字符串,也不会在不同的字符串中比较这些字符串。
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()));
Try 尝试
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()
.AsEnumerable()
.Select(c => c.ToString()));
Since apparently the EF provider can't seem translate the ToString()
call into SQL, putting in a call to AsEnumerable()
will bring the query into memory and the ToString()
call will use LINQ to Objects. 由于显然EF提供程序似乎无法将
ToString()
调用转换为SQL,因此调用AsEnumerable()
会将查询带入内存,而ToString()
调用将使用LINQ to Objects。
Why not use ToString()? 为什么不使用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());
You should select string values from context.Items
to create an ObservableCollection<string>
by casting them to String
您应该从
context.Items
选择字符串值,以将其转换为String
来创建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.ToString()).Distinct());
Or create an ObservableCollection<decimal>
或创建一个
ObservableCollection<decimal>
ObservableCollection<decimal> cost =
new ObservableCollection<decimal>((from i in context.Items
where i.Cost != null
&& i.Cost > 0
orderby i.Cost
select i.Cost).Distinct());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.