LINQ:如何向 LINQ 查询添加多个变量?

发布于 2024-11-19 07:10:55 字数 919 浏览 6 评论 0原文

我有一个名为 List 的控制器操作,它采用以下参数

int Filer、int Field、int Operator、string QueryValue

public enum Filter
{
    ActiveEmployee,
    OnHoldEmployee,
    InactiveEmployee
}
public enum Field
{
    Name,
    ABRAID,
    JobTitle,
    LocationCode,
    Department
}
public enum Operator
{
    Contains,
    StartWith,
    EndWith,
    EqualTo
}

可能的情况:

Select employee where Name Equals "Value"

Select employee where Name EndWith with "Value"

Select employee where Name StartWith with "Value"

Select employee where JobTitle Contains "Value"

Select employee where Department Equals "Value"

我使用 LINQ 来查询数据库

Employees = from p in DB.Employees
            where p.AWCID.Contains(queryvalue)
            select p;

但这意味着我必须编写嵌套的 switch case 来覆盖所有情况。 覆盖 3 个变量需要 180 多行代码,

我不太了解 LINQ 语法支持多个值。

有什么想法吗? 谢谢。

I have a a controller action called List that take the following parameters

int Filer, int Field, int Operator, string QueryValue

public enum Filter
{
    ActiveEmployee,
    OnHoldEmployee,
    InactiveEmployee
}
public enum Field
{
    Name,
    ABRAID,
    JobTitle,
    LocationCode,
    Department
}
public enum Operator
{
    Contains,
    StartWith,
    EndWith,
    EqualTo
}

Possible cases:

Select employee where Name Equals "Value"

Select employee where Name EndWith with "Value"

Select employee where Name StartWith with "Value"

Select employee where JobTitle Contains "Value"

Select employee where Department Equals "Value"

I'm using LINQ to query the database

Employees = from p in DB.Employees
            where p.AWCID.Contains(queryvalue)
            select p;

But thats mean I have to wite nested switch case to cover all cases.
It's over 180 lines of code to cover 3 variables,

I'm not very aware with LINQ syntax to support multiple values.

Any thoughts?
Thanks.

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

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

发布评论

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

评论(2

不即不离 2024-11-26 07:10:55

假设查询只是一种类型的查询(contains、startswith 等),我会尝试这样的操作:

public IQueryable<Employee> GetEmployees(queryType, value)
{
    var Employees = from p in DB.Employees
            select p;

    // could use a switch statement here as well
    if (queryType == "contains")
        return Employees.where(e => e.contains(value));
    else if...
}

您还可以将查询类型设置为枚举。

Assuming the query will be only one type of query (contains, startswith, etc.), I would try something like this:

public IQueryable<Employee> GetEmployees(queryType, value)
{
    var Employees = from p in DB.Employees
            select p;

    // could use a switch statement here as well
    if (queryType == "contains")
        return Employees.where(e => e.contains(value));
    else if...
}

You could also make the query types an enum.

攒眉千度 2024-11-26 07:10:55

它确实使查询变得复杂。如果使用实体框架,您可以考虑实体 SQL,它的复杂性要低得多,因为您可以将整个查询动态构建为字符串。或者,LINQ to SQL 具有动态 LINQ,它也支持基于文本的查询...

It does complicate the query. If using Entity Framework, you could consider Entity SQL which would be a lot less complex as you can dynamically build the entire query as a string. Or, LINQ to SQL has Dynamic LINQ which also supports textual based queries...

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