Home
Help
Register
Log in

Search

 
   Active Threads  

You are here: Home > LLBLGen Pro > LLBLGen Pro Runtime Framework> LinqToLLBL and the Like SQL operator
 

Pages: 1
LLBLGen Pro Runtime Framework
LinqToLLBL and the Like SQL operator
Page:1/1 

  Print all messages in this thread  
Poster Message
omar
User



Location:
Salmiya, Kuwait
Joined on:
15-Oct-2004 15:44:57
Posted:
569 posts
# Posted on: 03-Jul-2011 11:33:22.  
We have a search screen in the UI where the user enters data and then the code should build a dynamic query to fetch the results.

In pre-LINQ era, this was done by building a filterBucket for any of the dirty UI controls and then sending this filterBucket to the BL to fetch the results.

In building the filterBucket, handling the Like operator was trivial as that is supported directly in LLBL's filtering syntax.

When trying to do the same with LINQ, we tried using the LINQ functions StartWith, EndsWith, and Contains instead of LLBL's Like. The problem is this would not support such cases when the text being filtered looks like "firstName%LastName" or "firstName%middleName%anotherMiddleName%LastName"

Is there a way to have the same support (or simulate that support) for LIKE in LLBL's filtering syntax to be also in LLBL's LINQ ?
We don't know who we are until we see what we can do  Top
daelmo
Support Team



Location:
Guatemala City
Joined on:
28-Nov-2005 23:35:24
Posted:
8098 posts
# Posted on: 03-Jul-2011 23:32:34.  
omar wrote:
Is there a way to have the same support (or simulate that support) for LIKE in LLBL's filtering syntax to be also in LLBL's LINQ ?

Hi Omar. This is not built-in, but you can easily write your own Function mapping to do that:

FunctionMappings
Code:
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace NW.LLBL.MSSQL.Adapter.v31.Tests
{
    /// Class which is used to specify the call to the LIKE construct.
    public class NorthwindFunctions
    {
        public static bool Like(string fieldToSearch, string pattern)
        {
            // empty body, as it's just here to make the query compile. The call is converted to a SQL function.
            return true;
        }
    }

    /// Class which defines the custom mapping between NorthwindFunctions.Lile and SQL LIKE function
    public class NorthwindFunctionMappings : FunctionMappingStore
    {
        public NorthwindFunctionMappings()
            : base()
        {
            // Field LIKE pattern
            this.Add(new FunctionMapping(typeof(NorthwindFunctions), "Like", 2, "({0} LIKE {1})"));
        }
    }
}


Usage

Code:
[TestMethod]
public void FilterWithLinqLikePattern()
{
    string searchPattern = "J%n";
    var customers = new List<CustomerEntity>();

    using (var adapter = new DataAccessAdapter())
    {
        var metaData = new LinqMetaData(adapter);
        metaData.CustomFunctionMappings = new NorthwindFunctionMappings();

        customers = (from c in metaData.Customer                        
                        where NorthwindFunctions.Like(c.ContactName, searchPattern)
                        select c).ToList();
    }

    Assert.AreEqual(2, customers.Count);
}


David Elizondo
LLBLGen'ing (articles and code snippets) | linkedin | twitter
 
Top
omar
User



Location:
Salmiya, Kuwait
Joined on:
15-Oct-2004 15:44:57
Posted:
569 posts
# Posted on: 03-Jul-2011 23:36:40.  
Thank you daelmo. I will give your function-mapping tip a try and get back to you
We don't know who we are until we see what we can do  Top
Pages: 1  


Powered by HnD ©2002-2007 Solutions Design
HnD uses LLBLGen Pro

Version: 2.1.12172008 Final.