Home > Software design >  how to use the condition field is null in Entity Framework Core (Oracle)
how to use the condition field is null in Entity Framework Core (Oracle)

Time:10-07

I need to use the condition "field is null" in linq, I tried using "field == null" but it doesn't bring me the records.

var query = from p in _context.Product
where p.PRODUCT_IND_CHANGE == null
select new ProductViewModel { Id = p.PRODUCT_ID, name = p.PRODUCT_NAME };

I made this small subquery to understand why it doesn't work.

SELECT 
(SELECT count(1)
        FROM PRODUCTS
        WHERE PRODUCT_IND_CHANGE != NULL
) AS SYMBOL_OPERATOR1
,(SELECT count(1)
        FROM PRODUCTS
        WHERE PRODUCT_IND_CHANGE IS NULL
) AS SYMBOL_OPERATOR2
FROM DUAL

query example

is there any way to use "where field is null" in linq or to simulate it?

librarys:

  • Microsoft.EntityFrameworkCore (version 6.0.8)
  • Oracle.EntityFrameworkCore (version 6.21.61)

UPDATE 1:

I just checked the log, and it's printing the following when transforming to pl sql code

2022-10-06 17:36:53.0309|DEBUG|Microsoft.EntityFrameworkCore.Query|192.168.1.15|Generated query execution expression: 
'queryContext => new SingleQueryingEnumerable<ProductViewModel>(
    (RelationalQueryContext)queryContext, 
    RelationalCommandCache.SelectExpression(
        Projection Mapping:
            Id -> 0
            Name -> 1
        SELECT s.PRODUCT_ID AS Id, s.PRODUCT_NAME AS Name
        FROM PRODUCT AS s
        WHERE s.PRODUCT_IND_CHANGE == NULL), 
    Func<QueryContext, DbDataReader, ResultContext, SingleQueryResultCoordinator, ProductViewModel>, 
    API.SGA.Products.Data.DbContext, 
    False, 
    False, 
    True
)'|

CodePudding user response:

did you try

where p.PRODUCT_IND_CHANGE.Equals(null)

in Linq?

In SQL it should be

WHERE PRODUCT_IND_CHANGE IS NOT NULL

CodePudding user response:

did you make sure that the database actually has null values or do you have string values that say null

  • Related