Home > Blockchain >  How to convert SQL Syntax to EF Core
How to convert SQL Syntax to EF Core

Time:10-11

SQL statement:

SELECT DISTINCT FUND_SOURCE
FROM [dbo].[PROPERTY_TRANSACTION]
WHERE FUND_SOURCE != ''
  AND FUND_SOURCE IS NOT NULL ;

I tried this EF Core code:

var predicate = PredicateBuilder.New<PROPERTYTRANSACTION>(true);
    var query = _ctx.PROPERTY_TRANSACTION
                    .Where(fs => !(fs.FUND_SOURCE == null && fs.FUND_SOURCE.Trim() == string.Empty))
                    .Distinct();
return query.Where(predicate);

This EF Core code returns all the rows from the database.

Can you please help me how to distinct all the row of the database?

CodePudding user response:

You are missing FUND_SOURCE -

var query = _ctx.PROPERTY_TRANSACTION
    .Where(fs => !(fs.FUND_SOURCE == null && fs.FUND_SOURCE.Trim() == string.Empty))
    .Select(fs => fs.FUND_SOURCE).Distinct();

CodePudding user response:

You can use IsNullOrEmpty

var result = _ctx.PROPERTY_TRANSACTION
    .Where(fs => !string.IsNullOrEmpty(fs.FUND_SOURCE))
    .Select(fs => fs.FUND_SOURCE)
    .ToList()
    .Distinct();
  • Related