Matthis Kohli
Matthis Kohli

Reputation: 1995

VB.NET LINQ Method Syntax disallows implicit conversions from 'Integer?' to 'Integer'

Compare weirdness when working with LINQ and Entity Framework. I want to retrieve an ID from my DB and I get this weird message.

I could simply fix it as you can see but I want to understand why this happens.

Question:

Why do I get this error message even if I check with "HasValue" or I use "FirstOrDefault"? It can't be null in my opinion but I obviously miss something.

Code and Error

Upvotes: 1

Views: 81

Answers (3)

Jonathan Allen
Jonathan Allen

Reputation: 70307

Add .Value if you are 100% sure the Integer? has a value.

Why do I get this error message even if I check with "HasValue"

Entity Framework just uses the objects you give it. It can't create a new object where OPX_ isn't nullable.

Upvotes: 2

Aimnox
Aimnox

Reputation: 899

The compiler is not perfect, we can see that OPX_Rechte will have a value because of the where statment, but for the compiler you are just using a the object cctUser that have an Integer? and it needs an Integer.

Upvotes: 1

wizzardmr42
wizzardmr42

Reputation: 1644

the setOpxRights function presumably takes an Integer as a parameter and Option Strict On won't allow an Integer? to be implicitly converted to an Integer. If you are sure that it will always have a value, pass in cctUser.OPX_Rechte.Value

Upvotes: 2

Related Questions