views:

4207

answers:

4
var items = from c in contacts
            select new ListItem
            {
                Value = c.ContactId, //Cannot implicitly convert type 'int' (ContactId) to 'string' (Value).
                Text = c.Name
            };
var items = from c in contacts
            select new ListItem
            {
                Value = c.ContactId.ToString(), //Throws exception: ToString is not supported in linq to entities.
                Text = c.Name
            };

Is there anyway I can achieve this? Note, that in VB.NET there is no problem use the first snippet it works just great, VB is flexible, im unable to get used to C#'s strictness!!!

A: 

Can you try:

var items = from c in contacts
        select new ListItem
        {
            Value = Convert.ToString(c.ContactId), 
            Text = c.Name
        };
Tony Heupel
Nope. doesn't work
Shimmy
A: 

My understanding is that you have to create a partial class to "extend" your model and add a property that is readonly that can utilize the rest of the class's properties.

public partial class Contact{

   public string ContactIdString
   {
      get{ 
            return this.ContactId.ToString();
      }
   } 
}

Then

var items = from c in contacts
select new ListItem
{
    Value = c.ContactIdString, 
    Text = c.Name
};
Mcbeev
No, you can't use custom properties in LINQ to Entities (in .NET 3.5).
Craig Stuntz
I didn't tested it, but it won't work either.since it's not a table field property.I could first do it with ToArray() then linqing over objects but I want to query the DB. I assume will not be able to do it.I created my own ListItem that takes an int field.That works better for me.
Shimmy
+4  A: 

I think the best solution is:

var collection = contacts.ToDictionary(i=> i.ContactId, i=> i.Name);
IEnumerable<ListItem> listItems = from i in collection
                                  select new ListItem
                                  {
                                      Value = Key.ToString(),
                                      Text = Value
                                  };

Or do it inline:

IEnumerable<ListItem> listItems = 
    from i in contacts.ToDictionary(i=> i.ContactId, i=> i.Name)
                                  select new ListItem
                                  {
                                      Value = Key.ToString(),
                                      Text = Value
                                  };
Shimmy
+7  A: 

With EF v4 you can use SqlFunctions.StringConvert. There is no overload for int so you need to cast to a double or a decimal. Your code ends up looking like this:

var items = from c in contacts
        select new ListItem
        {
            Value = SqlFunctions.StringConvert((double)c.ContactId),
            Text = c.Name
        };
Brian
Did you test it andit works (with linq 2 Entities, not 2 objects etc.)?
Shimmy
Worked for me, thanks Brian!
Why on earth wouldn't they include an overload for int?
Jeremy Coenen