在Dynamic LINQ中使用Contains时如何动态地将Cast类型转换为字符串? [英] How to Type Cast dynamically to string while using Contains in Dynamic LINQ?

查看:63
本文介绍了在Dynamic LINQ中使用Contains时如何动态地将Cast类型转换为字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用动态LINQ查询在类的所有属性中搜索一些文本.我正在使用以下函数来创建表达式.我正在将属性名称和搜索文本传递给该方法. 在该方法中,如果属性类型为String,那么它工作正常.如果属性类型为int,DateTime,GUID.那就行不通了.

I want to use Dynamic LINQ Query to Search with some text in all Properties in a class. i am using following function to create expression. I am passing property name and search text to the method. In that method if the property type is String then it is working fine. if the property type is int, DateTime, GUID. then it is not working.

我们知道,Contains方法仅适用于元素数组或字符串.我认为属性的值应类型转换为字符串.那么该怎么做呢?带有解释的解决方案是完整的帮助.

As we know Contains method only for array of elements or for string. I am thinking the value to property should type cast to string. So How to do it? Solution with Explanation is help full.

i从中收集了代码.

i Collected code from this.

   public static Expression<Func<T, bool>> ContainsExp<T>(string propertyName, string contains)
    {
        var parameterExp = Expression.Parameter(typeof(T), "type");
        var propertyExp = Expression.Property(parameterExp, propertyName);

      MethodInfo method = typeof(string).GetMethod("Contains", new[] { typeof(string) });


        var someValue = Expression.Constant(contains, typeof(string));
        var containsMethodExp = Expression.Call(propertyExp, method, someValue);

        return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);

    }

推荐答案

好吧,您可能知道不可能在linq中对实体使用ToString().

Well, you probably know that's it's not possible to use ToString() in linq to entities.

所以下面的问题是:如何将其他类型转换为字符串.

So the following question is : how can I convert other types to string.

对于数字值,您具有SqlFunctions.StringConvert,但是对于double?decimal?

For numeric values, you have SqlFunctions.StringConvert, but it has only overloads for double? and decimal?

对于 DateTime ,您可以在DateTime上应用SqlFunctions.DatePart后使用SqlFunctions.StringConvert找到某些内容(这可能意味着至少需要3次调用SqlFunctions.DatePart,分别用于年,月,天)

For DateTime, you may find something using SqlFunctions.StringConvert after having applied SqlFunctions.DatePart on your DateTime (which probably means at least 3 call to SqlFunctions.DatePart, for year, month, day)

对于向导,我认为没有办法直接做到这一点.一种方法(在数据库级别,如果您使用Sql Server)可能是拥有一个计算列.计算列可以存储GUID的varchar转换表示形式.也许有更好的方法.

For Guid, I don't think there's a way to do it directly. One way (at db level, if you use Sql Server) could be to have a Computed column. The computed column could store a varchar converted representation of your GUID. Maybe there's a better way.

无论如何,这至少是一个适用于integerstring的示例:

Anyway, here's at least a sample which should work for integer as well as string:

 public static Expression<Func<T, bool>> ContainsExp<T>(string propertyName, string contains)
        {

            //first, get the type of your property
            var propertyType = typeof(T).GetProperty(propertyName).PropertyType;
            //no change
            var parameterExp = Expression.Parameter(typeof (T), "type");
            Expression propertyExp = Expression.Property(parameterExp, propertyName);
            //if property's type is int
            if (propertyType == typeof (int))
            {
                //convert your Expression to a nullable double (or nullable decimal),
                //so that you can use SqlFunctions.StringConvert
                propertyExp = Expression.Convert(propertyExp, typeof (double?));
                //get the SqlFunctions.StringConvert method for nullable double
                var stringConvertMethod = typeof (SqlFunctions).GetMethod("StringConvert", new[] {typeof (double?)});
                //call StringConvert on your converted expression
                propertyExp = Expression.Call(stringConvertMethod , propertyExp);
            }
            //no change
            var method = typeof (string).GetMethod("Contains", new[] {typeof (string)});


            var someValue = Expression.Constant(contains, typeof (string));
            var containsMethodExp = Expression.Call(propertyExp, method, someValue);

            return Expression.Lambda<Func<T, bool>>(containsMethodExp, parameterExp);

        }

这篇关于在Dynamic LINQ中使用Contains时如何动态地将Cast类型转换为字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆