当存在不会使用 LinQ 的 where 部分中的所有字段的条件时,如何创建动态查询

发布于 2024-12-11 05:30:55 字数 1411 浏览 0 评论 0原文

我正在使用数据库中的数据实体和域服务。

我使用 .net 生成的代码进行简单查询,就像这样

public IQueryable<employee> GetEmployeesByLocale(int localeID)
{
    return ObjectContext.employees.Where(e => e.Locale_ID == localeID);
}

现在,我需要相应地更改 .Where 部分,所以:

if (localeID > 0)
{
    ['Where' should be like .Where(e => e.Locale_ID == localeID)];
}
if (projectID > 0)
{
    [IF localeID == 0, 'Where' should be like .Where(e => e.Project_ID == projectID)
    Else if localeID > 0, Where should use both, sort of .Where(e => e.Locale_ID == localeID && e.Project_ID == projectID)];
}

等等与其他变量。

有很多可能的组合,这就是为什么我尝试使用 .Where(string,parameter[]) 的重载

string q = string.Empty;
if (localeID > 0)
{
    q = "Locale_ID = " + localeID.ToString();
}

if (projectID > 0)
{
    q = q == string.Empty ? "Project_ID = " + projectID.ToString() : q + " and " + "Project_ID = " + projectID.ToString();
}
... (for other variables and fields)
...
System.Data.Objects.ObjectParameter[] param = new System.Data.Objects.ObjectParameter[1];
param[0] = new System.Data.Objects.ObjectParameter("param", 1);
return ObjectContext.employees.Where(q, param);

但是,这只会给出一个错误,因为这样所有的字段名都被认为是out-范围外/不存在。即使在字符串中使用员工。[field_name],它们“不存在”

有谁知道在查询的 .Where 部分中使用条件的方法吗?或者如何创建一些包含我的查询的 var 或对象,以便我可以解析它?

I am using a Data Entity from the db, and a Domain Service.

I am using .net's generated code for the simple queries, like this

public IQueryable<employee> GetEmployeesByLocale(int localeID)
{
    return ObjectContext.employees.Where(e => e.Locale_ID == localeID);
}

Now, I need to change the .Where section accordingly, so:

if (localeID > 0)
{
    ['Where' should be like .Where(e => e.Locale_ID == localeID)];
}
if (projectID > 0)
{
    [IF localeID == 0, 'Where' should be like .Where(e => e.Project_ID == projectID)
    Else if localeID > 0, Where should use both, sort of .Where(e => e.Locale_ID == localeID && e.Project_ID == projectID)];
}

and so on with other variables.

There are many possible combinations , which is why I was trying to use the overload for .Where(string, parameter[])

string q = string.Empty;
if (localeID > 0)
{
    q = "Locale_ID = " + localeID.ToString();
}

if (projectID > 0)
{
    q = q == string.Empty ? "Project_ID = " + projectID.ToString() : q + " and " + "Project_ID = " + projectID.ToString();
}
... (for other variables and fields)
...
System.Data.Objects.ObjectParameter[] param = new System.Data.Objects.ObjectParameter[1];
param[0] = new System.Data.Objects.ObjectParameter("param", 1);
return ObjectContext.employees.Where(q, param);

However, this only gives an error, because then all the fieldnames are supposedly out-of-scope/non-existing. Even if use employees.[field_name] in the string, they "don't exist"

Does anyone know of a way to use conditionals inside the .Where part of the query? Or how to create some var or object containing my query so I can just parse it?

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

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

发布评论

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

评论(1

野鹿林 2024-12-18 05:30:55

您可以使用

IQueryable<Employee> emp =  ObjectContext.employees;
if (localeID > 0)  
    emp=emp.Where(e => e.Locale_ID == localeID).AsQueryable();  

if (projectID > 0)  
    emp=emp.Where(e => e.Project_ID == projectID).AsQueryable();  

This 将连接 where 子句

You can use

IQueryable<Employee> emp =  ObjectContext.employees;
if (localeID > 0)  
    emp=emp.Where(e => e.Locale_ID == localeID).AsQueryable();  

if (projectID > 0)  
    emp=emp.Where(e => e.Project_ID == projectID).AsQueryable();  

This will concatenate the where clauses

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文