[Using LLBLGen Pro 3.1 with Entity Framework 4, .NET 4 and SQLServer 2005]
I've got a linq query that includes .Contain(keyword);
IEnumerable<Product> products = null;
using (var context = new ModelDataContext())
{
products = (from product in context.Products where product.Title.Contains(keyword)
select product);
}
I was looking into the performance of the query and discovered that when the SQL is generated it's actually a "like '%keyword%'" that is being generated not a contains.
After doing a bit of research I came across some info in the LLBLGen Pro documentation regarding FunctionMapping:
I've created a table-valued function on my sql database, and also the classes required within my project:
public class CustomDatabaseFunctions
{
public static bool FullTextSearch(string fieldToSearch, string toFind)
{
// empty body, as it's just here to make the query compile. The call is converted to a SQL function.
return true;
}
}
public class CustomDatabaseFunctionMappings : FunctionMappingStore
{
public CustomDatabaseFunctionMappings() : base()
{
this.Add(new FunctionMapping(typeof(CustomDatabaseFunctions),"FullTextSearch",1,"Product_FullTextSearch({0})","ProductDatabase","Resources"));
}
}
The next part of the documentation states that you need to pass the custom FunctionMappingStore to the LinqMetaData. In the example this is done as follows:
metaData.CustomFunctionMappings = new NorthwindFunctionMappings();
var q = from o in metaData.Order where o.CustomerId == "CHOPS"
select new { o.OrderId, OrderTotal = NorthwindFunctions.CalculateOrderTotal(o.OrderId, true) };
The problem I have is that I'm doing my linq query using the DataContext and I've no idea where the variable metaData comes from or how to use it!
I'll keep looking to see if I can find out but any help very welcome!