将linq结果转换为ObservableCollection <string> </string>

时间:2013-03-27 15:25:36

标签: c# linq

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

我看了here,但这对我帮助不大。

更新

我尝试过以下操作但没有成功:

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.

5 个答案:

答案 0 :(得分: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()

答案 1 :(得分: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())); 。这样就不会创建如此多的字符串并在不同的字符串中比较这些字符串。

{{1}}

答案 2 :(得分: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,因此调用AsEnumerable()会将查询带入内存,ToString()调用将使用LINQ对象。

答案 3 :(得分: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());

答案 4 :(得分: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());