0

尝试将 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.

4

5 回答 5

2

转换Cost为字符串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());

调用时使用任何CultureInfo你需要的东西,如果有的话ToString()

于 2013-03-27T15:27:30.637 回答
2

ToString 之后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()));
于 2013-03-27T15:34:43.663 回答
2

尝试

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()));

由于显然 EF 提供程序似乎无法将ToString()调用转换为 SQL,因此调用 toAsEnumerable()会将查询带入内存,并且ToString()调用将使用 LINQ to Objects。

于 2013-03-27T17:34:43.513 回答
1

为什么不使用 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());
于 2013-03-27T15:28:01.510 回答
0

您应该通过将它们转换为来选择字符串值context.Items来创建ObservableCollection<string>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());

或者创建一个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());
于 2013-03-27T15:29:24.140 回答