如何让所有标题匹配C#titleNames中至少一个单词

我必须使用Entity Framework和Lamda表达式获取titleName匹配“searchText”字符串中至少一个单词的所有标题.当有空格并将结果存储在数组中时,我将分割searchText.我正在编写以下代码但无法获得所需的输出.

var searchTextArray = searchText.Split(' ');
var result = entities.titles.Where(x => searchTextArray.Contains(x.titleName));

假设searchText包含“Loneliness Management”字符串,那么我想获得包含titleName的所有标题的任何单词.
在这种情况下,我应该得到titleNames为的标题

Some loneliness affect all
Loneliness is painful
School Management
Proper management

更新1:
上面的查询生成SQL为

SELECT * FROM titles  WHERE (LOWER(titleName) IN (N'loneliness', N'management')) AND (LOWER(titleName) IS NOT NULL)

但我想生成像as的查询

SELECT * FROM titles WHERE titleName like N'%loneliness%' or titleName like N'%management%'

我在上面的表达中缺少什么?

更新2

我得到了@ 3dGrabber答案的预期输出但仍然生成的SQL查询比T-SQL中的LIKE运算符有点乱.非常感谢你们.生成的SQL现在就像

SELECT 
    *
    FROM [dbo].[titles] AS [Extent1]
    WHERE  EXISTS (SELECT 
        1 AS [C1]
        FROM  (SELECT 
            N'loneliness' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable1]
        UNION ALL
            SELECT 
            N'management' AS [C1]
            FROM  ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1]
        WHERE ( CAST(CHARINDEX([UnionAll1].[C1], LOWER([Extent1].[titleName])) AS int)) > 0
    )

最佳答案 不确定EF是否可以转换所有使用的操作,但这是我在Linq-to-Objects中要做的事情:

var searchText = "Loneliness Management";
var searchTerms = searchText.ToLower().Split(' ');

var entities = new [] 
{
    "Some loneliness affect all",
    "Loneliness is painful",
    "School Management",
    "No match here",
    "Maybe the next one?",
    "Proper management"
 };

var titles = entities.Select(t => t.ToLower());

var result = titles.Where(t => searchTerms.Any(s => t.Contains(s)));

如果您需要不区分大小写的搜索,请确保EF支持它,或者首先将数据小写放入DB中.否则你可能会遇到性能问题.

点赞