How do you check if a string contains any strings from a list in Entity Framework?

后端 未结 4 454
半阙折子戏
半阙折子戏 2020-12-06 06:38

I am trying to search a database to see if a string contains elements of a list of search terms.

var searchTerms = new List { \"car\", \"232\"          


        
4条回答
  •  伪装坚强ぢ
    2020-12-06 07:16

    You can try using Any method, I'm not sure whether it's supported but it's worth trying:

    var result = context.Data.Where(data => searchTerms.Any(x => data.Name.Contains(x)) ||
                                            searchTerms.Any(x => data.Code.Contains(x));
    

    If this gives you NotSupportedException you can add AsEnumerable before Where to fetch all records and execute the query in memory rather than DB.

提交回复
热议问题