EF Core 和 LINQ - 如何在运行时使用字符串变量构建动态Where 子句?

OJB*_*JB1 3 c# linq .net-core-3.1

我读过这个问题的许多不同变体,我不敢相信我需要的解决方案是如此复杂,以至于需要使用额外的库和疯狂的技巧,希望不是!

在运行时,我的项目中的 LINQ 查询需要根据用户想要过滤的数据库表中的列数动态更改。在我的示例中,我首先展示了一个可工作的硬编码 LINQ 查询。下一个示例使用在运行时构建的列表,我需要弄清楚如何将字符串变量(whereClause)插入到 LINQ 查询中而不出现编译错误?

工作示例(硬编码)

logs = _context.Logs.Where(s => s.Level == LogLevel & s.LogEventCategory == EventCategory)
               .Select(s => new Logs()
               {
                   TimeStamp = s.TimeStamp,
                   Level = s.Level,
                   Exception = s.Exception,
                   LogEventCategory = s.LogEventCategory,
                   LogEventType = s.LogEventType,
                   LogEventSource = s.LogEventSource,
                   LogEventName = s.LogEventName,
                   LogUserName = s.LogUserName,
                   LogForename = s.LogForename,
                   LogSurname = s.LogSurname,
                   LogData = s.LogData
               });
Run Code Online (Sandbox Code Playgroud)

示例二 - 我想要修复和使用的解决方案......

首先创建一个列表,每次运行新查询时列表的内容都会更改,通过父 OnGet 方法作为变量传递的字符串将包含一个值并在字符串连接中使用,或者将为 null,因此不会添加到列表并在串联中使用。

第二个例子是我遇到编译错误的地方。

var filtersList = new List<string>();
        if (LogLevel != null)
        {
            filtersList.Add("s.LogLevel == LogLevel");
        }
        if (EventCategory != null)
        {
            filtersList.Add("s.EventCategory == EventCategory");
        }

        var whereClause = string.Join(" & ", filtersList.ToArray());


logs = _context.Logs.Where(s => whereClause) // *HERE I WANT TO USE THE STRING VARIABLE! not working
                   .Select(s => new Logs()
                   {
                       TimeStamp = s.TimeStamp,
                       Level = s.Level,
                       Exception = s.Exception,
                       LogEventCategory = s.LogEventCategory,
                       LogEventType = s.LogEventType,
                       LogEventSource = s.LogEventSource,
                       LogEventName = s.LogEventName,
                       LogUserName = s.LogUserName,
                       LogForename = s.LogForename,
                       LogSurname = s.LogSurname,
                       LogData = s.LogData
                   });
Run Code Online (Sandbox Code Playgroud)

我收到的错误是“无法将 lambda 表达式转换为预期的委托类型,因为块中的某些返回类型不能隐式转换为委托返回类型”等等

Mil*_*ney 5

您只需在具体化之前将 .Where() 调用添加到查询末尾即可:

query = _context.Logs.Select(s => new ..);

if (EventCategory != null) query = query.Where(e => e.EventCategory == EventCategory);
if (LogLevel != null) query = query.Where(e => e.LogLevel == LogLevel);

var items = query.ToList();
Run Code Online (Sandbox Code Playgroud)