如何使用contains在Linq中搜索字符串到SQL

时间:2014-03-13 19:34:21

标签: c# sql linq

我使用下面的代码搜索多列中的特定字符串

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))

但这不允许我搜索

有人可以告诉我该怎么办?

2 个答案:

答案 0 :(得分:1)

根据您生成的查询,我认为您使用的是linq-to-sql。您可以使用SqlMethods.Like从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, "%");

答案 1 :(得分:1)

我已经创建了一个可以帮助你的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 page以获取更多详细信息

希望这有帮助