EF Core 排除多列为 Null 或空的位置

EF Core Exclude Where Multiple Columns are Null or Empty

我有一个名为 ControlActivityChangeLog 的 SQL 服务器 table。 table 有列 CurrentValueNewValue,它们都是可为 null 的 nvarchar。如何查询所有 ControlActivityChangeLogs 但排除 CurrentValue 和 NewValue 为 null 或空的结果。如果 CurrentValue 为 null 或空,而 NewValue 不是,那么我需要该记录。

我尝试将其添加到以下查询中,但最终没有返回任何结果:

!x.ControlActivityChangeLogs.Any(y => string.IsNullOrEmpty(y.CurrentValue) && string.IsNullOrEmpty(y.NewValue))

这是我的问题:

var qry = _context.ControlActivities
        .Include(x => x.ControlActivityChangeLogs)
        .Include(x => x.Company)
        .Where(x => 
            !x.IsDeleted && 
            !x.IsArchived && 
            !x.ControlActivityChangeLogs.Any(y => string.IsNullOrEmpty(y.CurrentValue) && string.IsNullOrEmpty(y.NewValue)))
        .AsQueryable();


    **OTHER COLUMN FILTERS HERE**


    var results = await qry.Select(x => new ModifiedCAModel
            {
                **List Of Columns**,
                Changes = x.ControlActivityChangeLogs
                    .Where(y => y => y.ControlActivityIssueId == null && y.ControlActivityTestId == null)
                    .OrderByDescending(y => y.ChangedDate)
                    .ToList()
            }).ToListAsync();
the below should be able to translate to SQL correctly

var qry = _(context.ControlActivities
    .Include(x => x.ControlActivityChangeLogs)
    .Include(x => x.Company)
    .Where(x => !x.IsDeleted 
             && !x.IsArchived 
             && x.ControlActivityChangeLogs.CurrentValue != null 
             && x.ControlActivityChangeLogs.CurrentValue != ""
             && x.ControlActivityChangeLogs.NewValue != null 
             && x.ControlActivityChangeLogs.NewValue != ""
    ).AsQueryable();

你应该检查 sql 正在执行什么!!!

这可以获取,但将日志记录添加到 ef。

WhereAny条件是针对包括的,所以需要

exclude results where CurrentValue and NewValue are null or empty

也就是

include 结果,其中 CurrentValue or NewValue is not null or空

即而不是

!x.ControlActivityChangeLogs.Any(y => string.IsNullOrEmpty(y.CurrentValue) && string.IsNullOrEmpty(y.NewValue)))

你需要

x.ControlActivityChangeLogs.Any(y => !string.IsNullOrEmpty(y.CurrentValue) || !string.IsNullOrEmpty(y.NewValue)))