LIKE operator

Posts   
 
    
neilx
User
Posts: 267
Joined: 02-Nov-2007
# Posted on: 24-Oct-2010 21:17:25   

In Linq to LLB how would I achieve a LIKE?

E.g.: in SQL: WHERE attribute LIKE 'LLB%'

I tried: where a.attribute.Contains("LLB")

but that doesn't work.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 25-Oct-2010 03:15:21   

neilx wrote:

In Linq to LLB how would I achieve a LIKE?

E.g.: in SQL: WHERE attribute LIKE 'LLB%'

For LIKE 'LLB%' you can use attribute.StartsWith("LLB"). If you want LIKE '%LLBL%' you can use Contains.

If that doesn't work please post your full relevant code and the generated sql for that query. Also make sure you are using the latest build.

David Elizondo | LLBLGen Support Team
neilx
User
Posts: 267
Joined: 02-Nov-2007
# Posted on: 25-Oct-2010 07:39:21   

RuntimeVersion: 2.6.0.0 RuntimeBuild: 04212010 Adapter template Windows 7

Indeed that works. Sorry for not posting code as that shows I really meant using a variable instead of a literal:

        public IQueryable<DomainNameEntity> ValidateUser(string clientUrl, string emailAddress)
        {
            var domainNameEntities = from a in metaData.DomainName
                                     where a.Client.ClientName == clientUrl && emailAddress.Contains(a.EmailDomain)
                                     select a;
            return domainNameEntities;
        }

This gives the error message: "Only constant operands are supported for calls to Contains" as shown in the trace below:

Start time: 07:32
Verifying test files.
Initializing the test runner.
Running the tests.
Test 'Enhesa.Applications.Clients.ClassicAspClientRedirection.OtherTests/ModelTests/ValidateUserWithValidEmailReturnsValid' failed:
    Execute
    SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryConstructionException: Only constant operands are supported for calls to Contains.
    RuntimeVersion: 2.6.0.0
    RuntimeBuild: 04212010
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleLikeExpression(LikeExpression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleBinaryExpressionBooleanOperator(BinaryExpression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleLambdaExpression(LambdaExpression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleWhereExpression(WhereExpression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleAggregateExpression(AggregateExpression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.GenericExpressionHandler.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.ExpressionHandlers.QueryExpressionBuilder.HandleExpression(Expression expressionToHandle)
    at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.HandleExpressionTree(Expression expression)
    at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.Execute(Expression expression)
    at SD.LLBLGen.Pro.LinqSupportClasses.LLBLGenProProviderBase.System.Linq.IQueryProvider.Execute[TResult](Expression expression)
    at System.Linq.Queryable.Count[TSource](IQueryable`1 source)
    IntegrationTesting\ModelTests.cs(67,0): at Enhesa.Applications.Clients.ClassicAspClientRedirection.OtherTests.IntegrationTesting.ModelTests.ValidateUserWithValidEmailReturnsValid()
Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Oct-2010 10:25:35   

LIKE expects a constant (string pattern), not a field value.

neilx
User
Posts: 267
Joined: 02-Nov-2007
# Posted on: 25-Oct-2010 11:40:25   

Walaa wrote:

LIKE expects a constant (string pattern), not a field value.

Oh dear. I use things in SQL alot like that:

... WHERE @emailAddress LIKE '%' + [EmailDomain]

How about a Replace? This seems to work:

        public IQueryable<DomainNameEntity> ValidateUser(string clientUrl, string emailAddress)
        {
            var domainNameEntities = from a in metaData.DomainName
                                     where a.Client.ClientName == clientUrl &&
                                       emailAddress.Replace(a.EmailDomain,String.Empty) != emailAddress
                                     select a;
            return domainNameEntities;
        }

and produces this SQL:

Query: SELECT TOP 1 COUNT(*) AS [LPAV_] FROM ( [dbo].[CLIENT] [LPA_L1]  INNER JOIN [dbo].[DomainName] [LPA_L2]  ON  [LPA_L1].[IDClient]=[LPA_L2].[IDClient]) WHERE ( ( ( ( ( [LPA_L1].[ClientName] = @ClientName1) AND ( REPLACE(@LO344820423, [LPA_L2].[EmailDomain], @LO2d2816fe4) <> @LPFA_12)))))
Parameter: @ClientName1 : AnsiString. Length: 50. Precision: 0. Scale: 0. Direction: Input. Value: "Demo".
Parameter: @LO344820423 : String. Length: 16. Precision: 0. Scale: 0. Direction: Input. Value: "anyone@valid.com".
Parameter: @LO2d2816fe4 : String. Length: 0. Precision: 0. Scale: 0. Direction: Input. Value: "".
Parameter: @LPFA_12 : String. Length: 16. Precision: 0. Scale: 0. Direction: Input. Value: "anyone@valid.com".

I'll use that unless you see any potential dangers with it. I can'tsimple_smile

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 25-Oct-2010 12:35:52   

No problem, if it works.