简体   繁体   English

用linq将字符串连接到实体

[英]Concat the string with linq to entities

I'm using the Dynamic Linq with Linq to Entities 我正在使用带有Linq to EntitiesDynamic Linq

So, I tested the normal List<Person> with this code and everything's work: 因此,我使用此代码测试了普通的List<Person> ,一切正常:

 List<Person> per = new List<Person>();
 per.Add(new Person { IdNo = "1", LastName = "test", MiddleName = "go", FirstName = "let" });
 per.Add(new Person { IdNo = "2", LastName = "hope", MiddleName = "is", FirstName = "way" });
 per.Add(new Person { IdNo = "3", LastName = "must", MiddleName = "be", FirstName = "human" });
 per.Add(new Person { IdNo = "4", LastName = "thing", MiddleName = "anywhere", FirstName = "can" });
 per.Add(new Person { IdNo = "5", LastName = "bird", MiddleName = "fly", FirstName = "away" });

 List<object> paramObjects = new List<object>();
 List<string> fields = new List<string>();
 fields.Add("IdNo == @0");
 paramObjects.Add("1");

 fields.Add("or (LastName + \", \" + MiddleName + \" \" + FirstName).Contains(@1)");
 paramObjects.Add("m");

 var where = string.Join(" ", fields.ToArray());
 var toParam = paramObjects.ToArray();

 var query = per.AsQueryable().Where(where, toParam).ToList();

and when I'm in Linq To Entities approach. 当我进入Linq To Entities方法时。

using(var con = new DbContext())
{
   var query = con.Person.Where(where, toParam).ToList();
}

I got this error: 我收到此错误:

LINQ to Entities does not recognize the method 'System.String Concat(System.Object, System.Object)' method, and this method cannot be translated into a store expression. LINQ to Entities无法识别方法'System.String Concat(System.Object,System.Object)',并且该方法无法转换为商店表达式。

My question is: 我的问题是:

How to Concat the string using the Linq To Entities base in my example using the Dynamic Linq ? 如何Concat使用字符串Linq To Entities基地,在使用动态的LINQ我的例子吗? Anyone can help me? 有人可以帮助我吗?

  • Option 1: 选项1:

    Change the line... 换线...

     fields.Add("or (LastName + \\", \\" + MiddleName + \\" \\" + FirstName).Contains(@1)"); 

    ...to ...至

     fields.Add("or string.Concat(LastName, \\", \\", MiddleName, \\" \\", FirstName).Contains(@1)"); 
  • Option 2: 选项2:

    Open the Dynamic.cs file of the Dynamic LINQ library, search for "GenerateStringConcat" in that file. 打开Dynamic LINQ库的Dynamic.cs文件,在该文件中搜索"GenerateStringConcat" You should find this method: 您应该找到此方法:

     Expression GenerateStringConcat(Expression left, Expression right) { return Expression.Call( null, typeof(string).GetMethod("Concat", new[] { typeof(object), typeof(object) }), new[] { left, right }); } 

    Change the object parameter types to string , that is: object参数类型更改为string ,即:

     Expression GenerateStringConcat(Expression left, Expression right) { return Expression.Call( null, typeof(string).GetMethod("Concat", new[] { typeof(string), typeof(string) }), new[] { left, right }); } 

Keep in mind that Dynamic LINQ is not a special "LINQ-to-Entities library", but a general library for IQueryable . 请记住,动态LINQ不是特殊的“ LINQ-to-Entities库”,而是IQueryable的常规库。 There might be Queryable providers that support string.Concat(object, object) with the general object parameter, but LINQ-to-Entities does not. 可能有可查询的提供程序支持带有通用object参数的string.Concat(object, object) ,但LINQ-to-Entities不支持。 It only supports the string parameter versions of string.Concat . 它仅支持string.Concatstring参数版本。 Looking through the Dynamic LINQ file when GenerateStringConcat is actually called (only once) I believe the change above is safe as long as you only want to use the library with LINQ-to-Entities/Entity Framework. 在实际调用GenerateStringConcat时(仅一次)浏览动态LINQ文件,我相信上述更改是安全的,只要您只想将库与LINQ-to-Entities / Entity Framework一起使用即可。

Edit 编辑

Option 1 does not work because Concat gets more than four string parameters in the example. 选项1不起作用,因为在示例中Concat获得了四个以上的字符串参数。 It only works up to four parameters because string.Concat has an overload with four explicit string parameters. 它最多只能使用四个参数,因为string.Concat具有四个显式string参数的重载。 It doesn't seem to be possible to use the overload with the array parameter params string[] in Dynamic LINQ. 在Dynamic LINQ中似乎无法将重载与数组参数params string[]一起使用。

I had the same problem but with NHibernate, my solution was to modify GenerateStringConcat Method 我有同样的问题,但是使用NHibernate,我的解决方案是修改GenerateStringConcat方法

I change this: 我改变这个:

static Expression GenerateStringConcat(Expression left, Expression right)
    {   
        return Expression.Call(
            null,
            typeof(string).GetMethod("Concat", new[] { typeof(object), typeof(object) }),
            new[] { left, right });
    }

For this: 为了这:

static Expression GenerateStringConcat(Expression left, Expression right)
    {            
        return Expression.Add(
            left,
            right,
            typeof(string).GetMethod("Concat", new[] { typeof(object), typeof(object) })
        );
    }

He has worked quite well :) 他工作得很好:)

Change 更改

 var where = string.Join(" ", fields.ToArray());

to

string where = string.Empty;
foreach(var f in fields)
{
    where += f + " "; 
}

where = where.Trim();

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

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