Reputation: 1568
I am using below code to search a particular string in multiple columns
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
};
where dc is DataContext object.
but this returns 0 results.
the query that is generated from above code is
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)
As soon as remove below search lines it works and returns ,
&& (eml.Username.Contains(strSearch)
|| eml.ProductURL.Contains(strSearch)
|| eml.Nickname.Contains(strSearch))
but this doesn't allow me to search
Can anyone please tell me how should I proceed?
Upvotes: 3
Views: 1060
Reputation: 7126
I have create a nuget package that can help you here. It will enable you to use the following syntax:
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
});
You can download the package from here
...also take a look at the GitHub page for more detailed information
Hope this helps
Upvotes: 1
Reputation: 38638
By your generated query, I think you are using the linq-to-sql
. You could use the SqlMethods.Like to generate the right like
query operator, from MSDN:
Determines whether a specific character string matches a specified pattern. This method is currently only supported in LINQ to SQL queries.
For sample:
// 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);
in your query statemant..
(SqlMethods.Like(eml.Username, strSearch)
|| SqlMethods.Like(eml.ProductURL, strSearch)
|| SqlMethods.Like(eml.Nickname, strSearch))
Otherwise you could add the %
char in your strSearch
string before your query to result a query with information in any part of string, for sample:
strSearch = string.Contat("%", strSearch, "%");
Upvotes: 1