System.String[] Split(Char[])' method unrecogn

2019-06-28 07:54发布

问题:

I have the following data and i want to filter the data using Linq to Entities, But i am getting the exception :

LINQ to Entities does not recognize the method 'System.String[] Split(Char[])' method, and this method cannot be translated into a store expression.

I have following data in the table

1           HPM,BKM     NULL        1,2,3
2           HPM,BKM     L1,L2       1,2
3           KK,CC,ZZ,PP             3,4

And i am writing the following code

var criteria_1="1";

var criteria_2="HPM,"

var Col4 = DB.Letter_Logic.Where(m => m.Col1.Equals(criteria_1)
            && m.Col2.Split(',').Contains(criteria_2)).ToList();

It should be give me values 1,2,3 as my result.

回答1:

This is because there is no way to translate Split(Char[]) into a SQL expression. You would need to do a ToList() first then perform your split operation, but beware that this will bring back more results to your computer before filtering them out using your split expression:

var Col4 = DB.Letter_Logic.Where(m => m.Col1.Equals(criteria_1))
                          .ToList()
                          .Where(m => m.Col2.Split(',').Contains(criteria_2))
                          .ToList();