Pergunta

The use of the null-coalescing operator returns an error: Operator '??' cannot be applied to operands of type 'int' and 'int'

When I hover over days it says (range variable) int days

var query = from date in dbdate.Dates
            join article in dbarticle.Articles on date.ArticleBarcode
                                               equals article.Barcode
            let days = article.Days
                    ?? dbgroup.Groups.Where(g => g.Number == article.GroupNumber)
                                .Select(g => g.Days).FirstOrDefault()

            where DbFunctions.AddDays(date.RunDate, days * -1) > DateTime.Now
            select article;
Foi útil?

Solução

You cannot store a null value into an int.

int a = null; // error !


What you want is a nullable int:

Nullable<int> a = null;

or

int? a = null;

Both are equivalent

In your example, article.Days must be nullable
You can configure it into your database and your entity will have nullable int for this property
Then, you will be able to use ?? on it in your linq query

Simple example:

int? a = null;
int b = a ?? 5; // if a is null then store 5, otherwise store value of a
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top