如何使用包含在Linq到SQL中搜索字符串
本文关键字:SQL 搜索 字符串 Linq 何使用 包含 | 更新日期: 2023-09-27 17:50:03
我使用下面的代码来搜索多个列中的特定字符串
IEnumerable<UserProductDetailResult> _query
= from eml in dc.UserProductDetails
join zk in dc.ZeekUsers on eml.aspnet_User.UserId equals zk.UserId
where eml.aspnet_User.LoweredUserName.Equals(strUserName.ToLower())
&& (eml.Username.Contains(strSearch)
|| eml.ProductURL.Contains(strSearch)
|| eml.Nickname.Contains(strSearch))
&& !eml.IsDeleted
&& eml.IsActive
select new UserProductDetailResult
{
_userProductDetail = eml,
_zeekUser = zk
};
其中dc为DataContext对象。
但是返回0个结果。
从上面代码生成的查询是
SELECT [t0].[UPID], [t0].[UserId], [t0].[PID], [t0].[Nickname], [t0].[Username], [t0].[ProductURL], [t0].[StartDt], [t0].[EndDt], [t0].[IsActive], [t0].[InfoData], [t0].[SocialNetworkingData], [t0].[AnalyticKey], [t0].[ProfileID], [t0].[UseDefaultAd], [t0].[UseDefaultEmail], [t0].[IsDeleted], [t0].[CreateDt], [t0].[LastUpdateDt], [t1].[ID], [t1].[UserId] AS [UserId2], [t1].[AccountType], [t1].[FirstName], [t1].[LastName], [t1].[Phone], [t1].[Address1], [t1].[Address2], [t1].[City], [t1].[State], [t1].[ZIP], [t1].[CountryID], [t1].[NickName1], [t1].[Nickname2], [t1].[AlternameEmail], [t1].[ProfileImage], [t1].[ZeekIdStatus], [t1].[RefZeekUserId], [t1].[IsActive] AS [IsActive2], [t1].[FailureCount], [t1].[IsBlocked], [t1].[IsFirstVisit], [t1].[IsWizardPassed], [t1].[IPAddress], [t1].[TimeZoneID], [t1].[CreateDt] AS [CreateDt2], [t1].[LastUpdateDt] AS [LastUpdateDt2]
FROM [dbo].[UserProductDetails] AS [t0]
INNER JOIN [dbo].[ZeekUsers] AS [t1] ON ((
SELECT [t2].[UserId]
FROM [dbo].[aspnet_Users] AS [t2]
WHERE [t2].[UserId] = [t0].[UserId]
)) = [t1].[UserId]
INNER JOIN [dbo].[aspnet_Users] AS [t3] ON [t3].[UserId] = [t0].[UserId]
WHERE ([t3].[LoweredUserName] = 'username') AND (([t0].[Username] LIKE 'a') OR ([t0].[ProductURL] LIKE 'a') OR ([t0].[Nickname] like 'a')) AND (NOT ([t0].[IsDeleted] = 1)) AND ([t0].[IsActive] = 1)
只要删除下面的搜索行,它就会工作并返回
&& (eml.Username.Contains(strSearch)
|| eml.ProductURL.Contains(strSearch)
|| eml.Nickname.Contains(strSearch))
但是这个不允许我搜索
谁能告诉我该怎么做?根据您生成的查询,我认为您正在使用linq-to-sql
。您可以使用SqlMethods。想要从MSDN生成正确的like
查询操作符:
示例:确定特定字符串是否与指定的模式。此方法目前仅在LINQ to SQL中受支持查询。
// first sample, any part of string
strSearch = string.Format("%[^a-zA-Z]{0}[^a-zA-Z]%", strSearch);
// on the end of the string
strSearch = string.Format("%[^a-zA-Z]{0}", strSearch);
//on the begining of the string
strSearch = string.Format("{0}[^a-zA-Z]%", strSearch);
在你的查询语句…
(SqlMethods.Like(eml.Username, strSearch)
|| SqlMethods.Like(eml.ProductURL, strSearch)
|| SqlMethods.Like(eml.Nickname, strSearch))
否则,您可以在查询之前在strSearch
字符串中添加%
字符,以得到包含字符串任何部分信息的查询,例如:
strSearch = string.Contat("%", strSearch, "%");
我已经创建了一个nuget包,可以在这里帮助您。它将使您能够使用以下语法:
var result = dc.UserProductDetails
.Where(eml => eml.IsActive && !eml.IsDeleted)
.Search(eml => eml.aspnet_User.LoweredUserName) // Search LoweredUsername
.IsEqual(strUserName) // when equals strUsername
.Search(eml => eml.UserName, // Search UserName
eml => eml.ProductURL, // OR ProductUrl
eml => eml.Nickname) // OR Nickname
.Containing(strSearch) // When contains strSearch
.Select(eml => new UserProductDetailResult // Build result...
{
_userProductDetail = eml
});
你可以从这里下载软件包
http://www.nuget.org/packages/NinjaNye.SearchExtensions
…还可以查看GitHub页面获取更多详细信息
希望能有所帮助