49227

LINQ error: The null value cannot be assigned to a member with type System.Int32 which is a non-null

I have following query:

var query = (from t1 in table1.Where(c => c.ServiceID == 2 && c.IsDeleted == 1) from t2 in table2.Where(c => c.RelServiceBonID == t1.RelServiceBonID && c.IsDeleted == 1).DefaultIfEmpty() from t3 in table3.Where(c => c.BonID == t1.BonID && c.UserID == 8 && c.IsDeleted == 1).DefaultIfEmpty() from t4 in table4.Where(c => c.BonID == t1.BonID && c.IsDeleted == 1) select new { t3.UserID, t2.SubServiceID, }); query.Dump();

When I run, I got the following error:

The null value cannot be assigned to a member with type System.Int32 which is a non-nullable value type.

The UserID field is primary key and I can not set that as nullable!

How I can pass this error?

Answer1:

The UserID field is primary key and I can not set that as nullable

As hvd explained below, because you effectively have an outer join, UserID can still be null. Besides that, SubServiceID can also be null. Usually it helps to cast the culprit to a nullable type:

select new { UserID = (int?)t3.UserID, SubServiceID = (int?)t2.SubServiceID });

Recommend

  • How to consolidate multiple sheets into a pivot table in Excel 2013
  • Find Min Value and paste in at the end of a table VBA
  • PHP - Loop through multiple tables and populate a multidimensional array
  • select statement inside insert statement in sql returns multiple rows
  • Complex sql query and JPQL
  • How to backup MySQL database using Query?
  • c# linq to sql iterating through left join results
  • LINQ How to Show Average of all results in GroupBy
  • How can I add a unique row number to my Linq select?
  • Generate repositories from EF Code First with created DbContext model classes
  • MySQL COUNT of multiple left joins - optomization
  • how do I fix this c++ typelist template compile error?
  • SQLite: full outer join with four tables
  • Keybinding keys to insert custom code Eclipse
  • MIPS questions about writing assembly to call functions on an array
  • The script does not work in IE. How can I fix it?
  • How to use case/esac in process substitution?
  • Excel changes conditional formatting formula
  • Aggregate case when inside non aggregate query
  • How to perform a left join in SQLALchemy?
  • Dynamically load images from project folder - Windows Phone 7
  • Image taken in Portrait is distorted when viewed in Landscape
  • Query with in a loop getting slower and slower
  • Laravel 5.2 Auth::check() on exception pages (layouts)
  • JsonMappingException: Can not deserialize instance of java.lang.Integer out of START_OBJECT token
  • Linq Full Outer Join on Two Objects
  • Why is django manage.py syncdb failing to create new columns on my development server?
  • How to concat Pandas dataframe columns
  • How to repeat sections of a SQL query across UNIONs? (DRY in SQL)
  • how to get username into sql trigger when multiple users signed on from asp membership
  • OSX - always hide certain files
  • Scrapy recursive link crawler
  • Spring Data JPA custom method causing PropertyReferenceException
  • NetLogo BehaviorSpace - Measure runs using reporters
  • Perl system calls when running as another user using sudo
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • EntityFramework adding new object to nested object collection