Null object cannot be converted to a value type.

Posts   
 
    
lubo278
User
Posts: 32
Joined: 10-Apr-2007
# Posted on: 10-Feb-2010 12:00:37   

Hi,

I have "wording" column which provides translate functionality. This column is nullable integer. I want to translate it "in-memory" (c# side).

This Linq query works fine

var q = from eventCat in md.EventCategory select Translate(eventCat.IdWordingIdentifier);

but this query (with anonymous constructor) does not

var q = from eventCat in md.EventCategory select new {A = Translate(eventCat.IdWordingIdentifier)};

it throws exception "Null object cannot be converted to a value type."

[InvalidCastException: Null object cannot be converted to a value type.] System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +7598766 System.Convert.ChangeType(Object value, Type conversionType) +32 lambda_method(ExecutionScope , Object[] , Int32[] ) +141 SD.LLBLGen.Pro.LinqSupportClasses.DataProjectorToObjectList1.AddRowToResults(IList projectors, Object[] rawProjectionResult) +104 SD.LLBLGen.Pro.LinqSupportClasses.DataProjectorToObjectList1.SD.LLBLGen.Pro.ORMSupportClasses.IGeneralDataProjector.AddProjectionResultToContainer(List1 valueProjectors, Object[] rawProjectionResult) +9 SD.LLBLGen.Pro.ORMSupportClasses.ProjectionUtils.FetchProjectionFromReader(List1 valueProjectors, IGeneralDataProjector projector, IDataReader datasource, Int32 maxNumberOfItemsToReturn, Int32 pageNumber, Int32 pageSize, Boolean clientSideLimitation, Boolean clientSideDistinctFiltering, Boolean clientSidePaging, UniqueList1 stringCache, Dictionary2 typeConvertersToRun) +1220 SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.GetAsProjection(List1 valueProjectors, IGeneralDataProjector projector, ITransaction transactionToUse, IRetrievalQuery queryToExecute, Dictionary2 typeConvertersToRun) +127 SD.LLBLGen.Pro.ORMSupportClasses.DaoBase.GetAsProjection(List1 valueProjectors, IGeneralDataProjector projector, ITransaction transactionToUse, IEntityFields fields, IPredicateExpression filter, IRelationCollection relations, Int32 maxNumberOfItemsToReturn, ISortExpression sortClauses, IGroupByCollection groupByClause, Boolean allowDuplicates, Int32 pageNumber, Int32 pageSize) +248 SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProvider.ExecuteValueListProjection(QueryExpression toExecute) +234 SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.ExecuteExpression(Expression handledExpression) +183 SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression) +23 SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute(Expression expression) +17 SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery1.Execute() +16 SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +16 System.Collections.Generic.List1..ctor(IEnumerable1 collection) +7665318 System.Linq.Enumerable.ToList(IEnumerable1 source) +61 ...

Translate method


  private string Translate(int id)
  {
    return "aa";
  }

It works fine if none of IDs in database is null.

I roughly understand why it does not work. The question is how to make it work simple_smile

lubo278
User
Posts: 32
Joined: 10-Apr-2007
# Posted on: 10-Feb-2010 12:14:23   

btw. I have latest version of LLBL (2.6.9.1106 - I just upgraded), still no improvement simple_smile DB is MS SQL

Walaa avatar
Walaa
Support Team
Posts: 14993
Joined: 21-Aug-2005
# Posted on: 10-Feb-2010 15:47:47   

What about this:

private string Translate(int? id)
{
    return "aa";
}
lubo278
User
Posts: 32
Joined: 10-Apr-2007
# Posted on: 10-Feb-2010 16:17:56   

heh,** it works**! It really didn't come to my mind.

Just for curiosity. I tried this


private string Translate(object id)
  {
    return "aa";
  }

and this does not work.

But nice "one char" solution simple_smile thanx