LINQ 查询动态过滤数据库,其中子表列等于可选输入值

发布于 2025-01-11 13:51:34 字数 1115 浏览 0 评论 0原文

因此,我有一种方法可以根据用户的可选输入动态更改 LINQ 查询。

  var query = from i in _dbContext.Incidents.Include(Incident => Incident.childtable1)
                                  .Include(Incident => Incident.childtable2)
                                  .Include(Incident => Incident.childtable3)
                                  .Include(Incident => Incident.childtable4)
                               
         
                        select i;

            // Append Query to take into account user selection

            query = query.
        if (bool)
            query = query.Where(i => i.column.Equals(userinput));
        if (bool)
            query = query.Where(i => i.column1.Equals(userinput1));
        if (bool)
            query = query.Where(i =>i.column2.Equals(userinput2));
       
            var results = query.ToList();


            return results;
        }
    

但是,我还希望能够过滤用户输入与子表之一中的列匹配的结果。

例如,

 if (bool)
            query = query.Where(i =>i.childtable2.column2.Equals(userinput2));

有没有办法将其添加到我当前的方法中?

So I have a method to dynamically alter a LINQ query based on optional inputs from the user.

  var query = from i in _dbContext.Incidents.Include(Incident => Incident.childtable1)
                                  .Include(Incident => Incident.childtable2)
                                  .Include(Incident => Incident.childtable3)
                                  .Include(Incident => Incident.childtable4)
                               
         
                        select i;

            // Append Query to take into account user selection

            query = query.
        if (bool)
            query = query.Where(i => i.column.Equals(userinput));
        if (bool)
            query = query.Where(i => i.column1.Equals(userinput1));
        if (bool)
            query = query.Where(i =>i.column2.Equals(userinput2));
       
            var results = query.ToList();


            return results;
        }
    

However I want to be able to also filter results where the user input matches a column in one of the child tables.

e.g something like

 if (bool)
            query = query.Where(i =>i.childtable2.column2.Equals(userinput2));

Is there a way to add it to my current method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半世蒼涼 2025-01-18 13:51:34

在这种情况下,你可以这样做:

if(condition)
    query = query.Where(i => i.ChildTable2.Any(c => c.Column2 == "foo"));

In this case you could do something like this:

if(condition)
    query = query.Where(i => i.ChildTable2.Any(c => c.Column2 == "foo"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文