为“like”创建表达式树在小数字段上

发布于 2024-09-18 12:36:53 字数 721 浏览 5 评论 0原文

我想为查询表达式创建一个表达式树,如下所示: 员工=> employee.Salary.StartsWith("28")

这样sql就可以显示为: 其中 (employee.salary like '28%')

问题在于,employee 对象的属性 Salary 是小数,而 StartsWith 不是小数的属性。我怎样才能抽出时间来做这件事。

我的错误表达式树语法如下:

var searchTextExp = Expression.Constant("28");
var parameterExp = Expression.Parameter(typeof(EmployeeEntity), "employee");
var propertyExp = Expression.Property(parameterExp, "Salary");
var startsWithExp = Expression.Call(propertyExp, "StartsWith", null, 
   searchTextExp);
Expression<Func<EmployeeEntity, bool>> searchExpr = 
   Expression.Lambda<Func<EmployeeEntity, bool>>
     (startsWithExp, new ParameterExpression[] { parameterExp });

I would like to create an expression tree for a query expression that looks something like this:
employee => employee.Salary.StartsWith("28")

So that the sql could appear as:
where (employee.salary like '28%')

The problem is that the property Salary of the employee object is a decimal and StartsWith is not a property of a decimal. How can i get around to do this.

My erroneous expression tree syntax is as follows:

var searchTextExp = Expression.Constant("28");
var parameterExp = Expression.Parameter(typeof(EmployeeEntity), "employee");
var propertyExp = Expression.Property(parameterExp, "Salary");
var startsWithExp = Expression.Call(propertyExp, "StartsWith", null, 
   searchTextExp);
Expression<Func<EmployeeEntity, bool>> searchExpr = 
   Expression.Lambda<Func<EmployeeEntity, bool>>
     (startsWithExp, new ParameterExpression[] { parameterExp });

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

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

发布评论

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

评论(3

旧情勿念 2024-09-25 12:36:53

您知道最简单的解决方案(可能也适用于 Linq to Sql)是:

employee => employee.Salary.ToString().StartsWith("28");

You know the easiest solution (that will probably also work in Linq to Sql) is:

employee => employee.Salary.ToString().StartsWith("28");
花期渐远 2024-09-25 12:36:53

我已经设法使用函数映射来解决这个问题,这是 LLBLGEN Pro 的一个功能。

public class Functions
{
    public static bool Like(string field, string value)
    {
        return true;
    }

    public static bool Like(decimal field, string value)
    {
        return true;
    }
}

public class FunctionMappings : FunctionMappingStore
{
    public FunctionMappings()
        : base()
    {
        FunctionMapping mapping = new FunctionMapping(
            typeof(Functions), 
            "Like", 
            2, 
            "{0} LIKE {1}");

        this.Add(mapping);
    }
}

然后,我将 FunctionMappings 的实例附加到 LINQ 元数据:

metadata.Mappings = new FunctionMappings();

然后按如下方式使用该函数:

employee => Functions.Like(employee.Salary,"28")

I have managed to solve this using Function Mappings which is a feature of LLBLGEN Pro.

public class Functions
{
    public static bool Like(string field, string value)
    {
        return true;
    }

    public static bool Like(decimal field, string value)
    {
        return true;
    }
}

public class FunctionMappings : FunctionMappingStore
{
    public FunctionMappings()
        : base()
    {
        FunctionMapping mapping = new FunctionMapping(
            typeof(Functions), 
            "Like", 
            2, 
            "{0} LIKE {1}");

        this.Add(mapping);
    }
}

I then attached an instance of FunctionMappings to the LINQ Metadata:

metadata.Mappings = new FunctionMappings();

Then used the Function as follows:

employee => Functions.Like(employee.Salary,"28")
酷炫老祖宗 2024-09-25 12:36:53

如果 llblgen 不支持 ToString(),您可以通过创建执行强制转换的视图来解决该问题。你的目标数据库是什么?

如果是 SQL Server,您可以使用 CAST() 函数来创建视图。

例如

  CREATE VIEW viewName as 
    SELECT CAST(Salary as varchar) as Salary
    FROM Employee

,之后您应该能够将该视图映射到一个新类并执行如上所述的查询。

employee = employee.Salary.StartsWith("28");

If llblgen does not support ToString() you can workaround the problem by creating a View that does the cast. What's your target database?

If it's SQL Server you can use the CAST() function to create the view.

e.g.

  CREATE VIEW viewName as 
    SELECT CAST(Salary as varchar) as Salary
    FROM Employee

After that you should be able to map that view against a new class and do the query like stated above.

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