LINQ查询中的合并操作员



使用null-coalescing操作员的使用返回错误: Operator '??' cannot be applied to operands of type 'int' and 'int'

当我悬停在days上时,它说(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;

you 不能将a null 值存储到int。

int a = null; // error !


您想要的是 nullable int:

Nullable<int> a = null;

int? a = null;

两者都是等效的

在您的示例中,article.Days必须是 nullable
您可以将其配置到数据库中,您的实体将对此属性具有无效的INT
然后,您将能够在LINQ查询中使用??

简单示例:

int? a = null;
int b = a ?? 5; // if a is null then store 5, otherwise store value of a

最新更新