简体   繁体   English

LINQ 2实体:将int与字符串进行比较

[英]LINQ 2 Entities: compare int to string

I need to construct a query to return a single entity based on a filter value. 我需要构造一个查询以基于过滤器值返回单个实体。 It must be executed on the server, LINQ 2 Objects is not an option. 它必须在服务器上执行,LINQ 2 Objects不是一个选项。

The type of entity to return, the property to get it by and the value of that property are all only known at runtime and can be changed at runtime, so I need to do this as dynamically as possible. 返回的实体类型,获得它的属性以及该属性的值仅在运行时才知道,并且可以在运行时进行更改,因此我需要尽可能动态地执行此操作。 I feel like I'm almost there, but that final step keeps eluding me. 我觉得自己快要到了,但是最后一步一直让我难以理解。 Here's what I've tried so far: 到目前为止,这是我尝试过的方法:

Model containts the filter data 模型包含过滤器数据

propParam will always be of type int propParam将始终为int类型

propModel will always be of type string propModel将始终为字符串类型

I can't change these types as this is just one of many filtering scenarios (a lookup). 我不能更改这些类型,因为这只是许多过滤方案(查找)中的一种。

private void SetFilter(Type typeToLookUp, string filterPropertyName)
{
    var propParam = typeToLookUp.GetProperty(filterPropertyName);
    var propModel = Model.GetType().GetProperty("FilterValue");

    var param = Expression.Parameter(typeToLookUp, "x");
    var bodyLeft = Expression.Property(param, propParam);
    var bodyRight = Expression.Property(Expression.Constant(Model), propModel);
    var body = Expression.Equal(bodyLeft, bodyRight);
    ...
}

This fails of course because I'm trying to compare an int (left) to a string (right). 当然这失败了,因为我试图将一个int(左)与一个字符串(右)进行比较。

So I need to convert the int to a string to be able to compare it, in LINQ 2 Entities SqlFunctions.StringConvert is used: 所以我需要将int转换为字符串以进行比较,在LINQ 2实体中使用SqlFunctions.StringConvert:

private void SetFilter(Type typeToLookUp, string filterPropertyName)
{
    var propParam = typeToLookUp.GetProperty(filterPropertyName);
    var propModel = Model.GetType().GetProperty("FilterValue");
    var stringConvertMethod = typeof(SqlFunctions).GetMethods(BindingFlags.Public | BindingFlags.Static)
                                .Single(
                                    x =>
                                    x.Name == "StringConvert" && x.GetParameters().Count() == 1 &&
                                    x.GetParameters()[0].ParameterType == typeof(double?));

    var param = Expression.Parameter(typeToLookUp, "x");
    var bodyLeft = Expression.Call(stringConvertMethod, Expression.Property(param, propParam));
    ...
}

Of course this method call will fail, because StringConvert doesn't take a parameter of type int. 当然,此方法调用将失败,因为StringConvert不采用int类型的参数。 So I need to be able to cast the value of propParam (an integer) to double before I pass it to the StringConvert method. 因此,在将其传递给StringConvert方法之前,我需要能够将propParam(整数)的值转换为双精度。 How would I go about doing that? 我将如何去做?

If I could just write the query it would look like this for example (this works): 如果我只可以编写查询,则示例如下所示(可行):

var result = Repository.Query<Customer>().Where(x => SqlFunctions.StringConvert((double)x.Id) == Model.FilterValue);

Maybe it would be easier to just use Dynamic LINQ to do this, or write a raw SQL query, but I would like to know if there's a solution the way I've been trying first. 也许只使用Dynamic LINQ来执行此操作,或者编写原始SQL查询会更容易,但是我想知道是否有一种我一直尝试的解决方案。

You can convert the int to a double in the expression by using Expression.Convert : 您可以使用Expression.Convertint转换为表达式中的double

var param = Expression.Parameter(typeToLookUp, "x");
var property = Expression.Property(param, propParam);  
var doubleValue = Expression.Convert(property, typeof(double));
var bodyLeft = Expression.Call(stringConvertMethod, doubleValue);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM