-1

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?

  • `FUND_SOURCE != '' AND FUND_SOURCE IS NOT NULL` <> `!(fs.FUND_SOURCE == null && fs.FUND_SOURCE.Trim() == string.Empty)`. Is a column likely to be both `null` _and_ `Empty` for a row? – HABO Oct 10 '22 at 03:13
  • Ref: [De Morgan's laws](https://en.wikipedia.org/wiki/De_Morgan's_laws). – HABO Oct 10 '22 at 13:29
  • Perhaps my [SQL to LINQ Recipe](https://stackoverflow.com/questions/49245160/sql-to-linq-with-multiple-join-count-and-left-join/49245786#49245786) might help you. – NetMage Oct 10 '22 at 23:32

2 Answers2

0

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();
Felix
  • 9,248
  • 10
  • 57
  • 89
  • Hi @Felix I updated the code. I tried your code but there's an error saying https://learn.microsoft.com/en-us/dotnet/csharp/misc/cs1503?f1url=%3FappId%3Droslyn%26k%3Dk(CS1503) – Pearly Shelly Oct 10 '22 at 05:30
  • you don't say anything about types or type conversion... How is that error relevant to the code in question? – Felix Oct 10 '22 at 21:22
0

You can use IsNullOrEmpty

var result = _ctx.PROPERTY_TRANSACTION
    .Where(fs => !string.IsNullOrEmpty(fs.FUND_SOURCE))
    .Select(fs => fs.FUND_SOURCE)
    .ToList()
    .Distinct();
Darkk L
  • 889
  • 1
  • 4
  • 14