Asked  7 Months ago    Answers:  5   Viewed   57 times
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!!!

 Answers

50

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).Trim(),
                Text = c.Name
            };
Tuesday, June 1, 2021
 
Fanda
answered 7 Months ago
11

you could write a little function

string int_array_to_string(int int_array[], int size_of_array) {
  string returnstring = "";
  for (int temp = 0; temp < size_of_array; temp++)
    returnstring += itoa(int_array[temp]);
  return returnstring;
}

untested!

a slightly different approach

string int_array_to_string(int int_array[], int size_of_array) {
  ostringstream oss("");
  for (int temp = 0; temp < size_of_array; temp++)
    oss << int_array[temp];
  return oss.str();
}
Tuesday, August 3, 2021
 
EastSw
answered 4 Months ago
45

Since you are materializing your query to list anyway, you could do the conversion on the .NET side, rather than in the RDBMS, like this:

...
select new {
   rn.ReleaseTitle,
   plat.MediaPlatformName,
   pub.MediaPublisherName,
   c.CountryName,
   rd.ReleaseDateName,
   rd.ReleaseDate,
   a.AffiliateLinkAddress
}).AsEnumerable() // <<== This forces the following Select to operate in memory
.Select(t => new {
   t.ReleaseTitle,
   t.MediaPlatformName,
   t.MediaPublisherName,
   t.CountryName,
   ReleaseDate = t.ReleaseDateName ?? t.ReleaseDate.ToString()
   t.AffiliateLinkAddress        
}).ToList();

Since the ToString() is called on an element from IEnumerable<T>, it will no longer fail. Also note the use of ?? operator in place of a null-checking ? : conditional.

Tuesday, August 17, 2021
 
Karsten
answered 4 Months ago
85

To update an existing but disconnected object, you need to "attach" it do the data context. This will re-use the existing primary key etc. You can control how to handle changes- i.e. treat as dirty, or treat as clean and track future changes, etc.

The Attach method is on the table - i.e.

ctx.Customers.Attach(customer); // optional bool to treat as modified
Wednesday, August 18, 2021
 
Jame
answered 4 Months ago
97

Convert your column values with NaN into 0 then typcast that column as integer to do so.

df[['class_parent_ref']] = df[['class_parent_ref']].fillna(value = 0)
df['class_parent_ref'] = df['class_parent_ref'].astype(int)

Or in reading your file, specify keep_default_na = False for pd.read_excel() and na_filter = False for pd.read_csv()

Wednesday, October 13, 2021
 
Esaevian
answered 2 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share