-4

My question is part of this problem:

I recieve a collection of id's from a form. I need to get the keys, convert them to string and pass it to the db.

 using (var db = new DbAmsecEntities())
 {
    cashsafelist = (from safe in db.Cashsafes
                    where safe.StoreId == (decimal)Convert.ToInt64(ddlLocationLists.SelectedValue)
                    select safe.CashsafeId).ToList();
    cashsafevalues = cashsafelist.Select(x => x.ToString).ToList();//getting error here
 } 
SparAby
  • 547
  • 1
  • 9
  • 25

2 Answers2

7

You need to use () in ToString

cashsafevalues = cashsafelist.Select(x => x.ToString()).ToList();
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
  • Or: cashsafelist.Select(ToString).ToList(); – Dennis_E May 28 '14 at 08:45
  • 1
    @Dennis_E: than won't compile since `ToString` is not static. – Tim Schmelter May 28 '14 at 08:46
  • @TimSchmelter being static has nothing to do with it. Can't you do that with an instance method??? On mono, I get the message it cannot infer the type, so that's why it fails. (I don't have access to a Windows machine right now, so I can't check if this is mono only). When you write a lambda like that, Resharper will hint to change it to a method group, because the x => x is redundant. – Dennis_E May 28 '14 at 09:16
  • @Dennis_E: you're right, whether it is `static` or not is not the only factor that matters. I must admit that i'm not sure how to get it to work with non-static methods but it must work somehow. That's why i've asked [this question](http://stackoverflow.com/questions/23908548/how-to-use-existing-method-instead-of-lambda-when-its-not-static) :) – Tim Schmelter May 28 '14 at 10:17
2

You can also use List.ConvertAll which was available since 2.0.

cashsafevalues = cashsafelist.ConvertAll<string>(d => d.ToString());

It can be more efficient than ToList because the list will be initialized with the correct size.

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939