LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method,

前端 未结 6 1977
没有蜡笔的小新
没有蜡笔的小新 2020-12-04 02:17

I am trying to implement a method where the keywords stored in the database for an activity (split by a comma) match the giving string split by a comma.

publ         


        
6条回答
  •  温柔的废话
    2020-12-04 02:40

    LINQ to Entities tries to translate your LINQ query into SQL. Since it doesn't know how to do String.Split in a SQL query, it fails.

    This means that unless you want to write a SQL implementation of String.Split, you can only do it in LINQ to objects, which means you need to load all of your data into memory first, then do the where clause. One easy way to do this is using .ToList():

    var results  = (from a in Entities.TblActivities select a).ToList(); //Results are now in memory
    results = results.Where(a =>
         a.Keywords.Split(',').Any(p => keyword.Contains(p))).ToList(); //This uses LINQ-to-objects
    

提交回复
热议问题