gpt4 book ai didi

c# - 如何将 SQL 'LIKE' 与 LINQ to Entities 一起使用?

转载 作者:IT王子 更新时间:2023-10-29 04:12:17 25 4
gpt4 key购买 nike

<分区>

我有一个文本框,允许用户指定搜索字符串,包括通配符,例如:

Joh*
*Johnson
*mit*
*ack*on

在使用 LINQ to Entities 之前,我有一个存储过程,它将该字符串作为参数并执行:

SELECT * FROM Table WHERE Name LIKE @searchTerm

然后我会在传入之前执行 String.Replace('*', '%')。

现在我正尝试使用 LINQ to Entities 来完成同样的事情。我知道有 StartsWith、EndsWith 和 Contains 支持,但它不会以我需要的方式支持它。

我阅读了“SqlMethods.Like”并尝试了这个:

var people = from t in entities.People
where SqlMethods.Like(t.Name, searchTerm)
select new { t.Name };

但是我得到以下异常:

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

我如何使用 LINQ to Entities 获得相同的功能?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com