Michel
Michel

Reputation: 23645

troubles creating a List of doubles from a list of objects

i have a list with objects. The object has a property 'Sales' which is a string. Now i want to create a list of doubles with the values of all objects' 'Sales' properties.

I tried this: var tmp = from n in e.Result select new{ Convert.ToDouble ( n.Sales) };

but this gives me this error:

Error 106 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.

EDIT: first i tried it without the Convert, but then i have a list of anonymous types (not strings) and i couldn't get that converted to a list fo doubles either....

Upvotes: 0

Views: 142

Answers (3)

Daniel Renshaw
Daniel Renshaw

Reputation: 34187

The following will give you a list of doubles.

List<double> listOfDoubles = (from n in e.Result
                              select Convert.ToDouble(n.Sales)).ToList();

Upvotes: 4

David Neale
David Neale

Reputation: 17048

Try this:

var tmp = from n in e.Result select new{ Sales = Convert.ToDouble ( n.Sales) };

Upvotes: 2

Andras Zoltan
Andras Zoltan

Reputation: 42363

Change your code to this:

var tmp = from n in e.Result select new{Value = Convert.ToDouble ( n.Sales) };

You need to define a property name for the anon type: i.e. "Value = blah"

Upvotes: 2

Related Questions