[Entity Framework 4、.NET 4、SQLServer 2005 で LLBLGen Pro 3.1 を使用]
.Contain(keyword); を含む linq クエリがあります。
IEnumerable<Product> products = null;
using (var context = new ModelDataContext())
{
products = (from product in context.Products where product.Title.Contains(keyword)
select product);
}
クエリのパフォーマンスを調べていたところ、SQL が生成されると、実際には "like '%keyword%'" が生成され、contains ではないことがわかりました。
少し調査した後、LLBLGen Pro のドキュメントで FunctionMapping に関する情報をいくつか見つけました。
SQL データベースにテーブル値関数を作成し、プロジェクト内で必要なクラスも作成しました。
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"));
}
}
ドキュメントの次の部分では、カスタム FunctionMappingStore を LinqMetaData に渡す必要があると述べています。例では、これは次のように行われます。
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) };
私が抱えている問題は、DataContext を使用して linq クエリを実行していて、変数 metaData がどこから来ているのか、またはその使用方法がわからないことです!
私は見つけることができるかどうかを確認し続けますが、どんな助けも大歓迎です!