使用 LLBLGen 时如何在 Linq 查询中进行全文搜索

发布于 2024-12-05 10:28:16 字数 1821 浏览 1 评论 0 原文

[将 LLBLGen Pro 3.1 与 Entity Framework 4、.NET 4 和 SQLServer 2005 结合使用]

我有一个 linq 查询,其中包括 .Contain(keyword);

    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 的信息:

http://www.llblgen.com/documentation/2.6/Using%20the%20 generated%20code/Linq/gencode_linq_functionmappings.htm

我在 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 查询,但我不知道变量元数据来自哪里或如何使用它!

我会继续寻找是否能找到答案,但非常欢迎任何帮助!

[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:

http://www.llblgen.com/documentation/2.6/Using%20the%20generated%20code/Linq/gencode_linq_functionmappings.htm

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

半衬遮猫 2024-12-12 10:28:16

您链接到的文档适用于我们的框架,但您却说您正在使用 EFv4。所以你应该使用 EF 的函数映射功能,而不是我们自己的框架;)。也就是说,如果您正在使用 EF,但代码建议您不要这样做。所以我很困惑。

最好在我们自己的支持论坛上发布有关 LLBLGen Pro 的问题,因为我们不对此进行监控。

the documentation you're linking to is for our framework, yet you're saying you're using EFv4. So you should use the function mapping feature of EF, not our own framework ;). That is, if you're using EF, but the code suggests you don't. So I'm very confused.

It's also better to post questions about LLBLGen Pro on our own support forums as we don't monitor SO.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文