0

Trying to store results from a LINQ query into ObservableCollection but the results from linq are of decimal type.

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.

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:
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.

Community
  • 1
  • 1
Robert
  • 1,696
  • 3
  • 36
  • 70

5 Answers5

2

Convert Cost to a string with 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());

Use whatever CultureInfo you need, if any, when calling ToString().

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
2

Do the ToString after the Distinct. 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()));
weston
  • 54,145
  • 21
  • 145
  • 203
  • I tried this out but it crashes silently when I run it. If I put a breakpoint on it and try to step-in/over it, the program just continues running and skips the rest of the code (as in the form loads up but there is nothing in the collection). – Robert Mar 27 '13 at 16:02
2

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.

Mike Cofoed
  • 151
  • 1
  • 4
1

Why not use 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());
wageoghe
  • 27,390
  • 13
  • 88
  • 116
0

You should select string values from context.Items to create an ObservableCollection<string> by casting them to 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> cost = 
    new ObservableCollection<decimal>((from i in context.Items
                                      where i.Cost != null
                                      && i.Cost > 0
                                      orderby i.Cost
                                      select i.Cost).Distinct());
Dmitrii Dovgopolyi
  • 6,231
  • 2
  • 27
  • 44
  • I tried your first suggestion but I get the same results as I commented on in Weston's post. I can't use the decimal ObservableCollection unfortunately. – Robert Mar 27 '13 at 16:04