亚音速 2.1:我疯了吗?

发布于 2024-08-21 14:23:39 字数 1030 浏览 4 评论 0原文

我正在开发一个使用亚音速 2.1 的项目。对于简单的搜索查询,我有以下代码:

            DAL.ItemCollection coll = new DAL.ItemCollection();
        SubSonic.Select s = new SubSonic.Select();
        s.From(DAL.Item.Schema);
        s.Where("Title").Like("%" + q + "%").Or("Tags").Like("%" + q + "%").And("IsActive").IsEqualTo(true);

        if (fid > 0)
        {
            s.And("CategoryID").IsEqualTo(fid);
            Session["TotalSearchResults"] = null;
        }
        s.Top(maxitems);

        //We'll get the recordcount before paged results
        total = s.GetRecordCount();

        s.Paged(pageindex, pagesize);
        s.OrderDesc("Hits");
        s.OrderDesc("Points");
        s.OrderDesc("NumberOfVotes");
        coll = s.ExecuteAsCollection<DAL.ItemCollection>();

现在的问题是,当我的 fid (FilterId) 大于 0 时,CategoryID 过滤器不起作用。它到达断点没有问题,但它没有被过滤。它与 LIKE 查询有关吗?如果我删除 if 部分和当前 s.where 并更改查询,它会完美地工作:

s.Where("CategoryID").IsEqualTo(fid);

我在这里错过了一些重要的东西吗?

亲切的问候, 标记

Im working on a project using subsonic 2.1. For a simple search query i've the following code:

            DAL.ItemCollection coll = new DAL.ItemCollection();
        SubSonic.Select s = new SubSonic.Select();
        s.From(DAL.Item.Schema);
        s.Where("Title").Like("%" + q + "%").Or("Tags").Like("%" + q + "%").And("IsActive").IsEqualTo(true);

        if (fid > 0)
        {
            s.And("CategoryID").IsEqualTo(fid);
            Session["TotalSearchResults"] = null;
        }
        s.Top(maxitems);

        //We'll get the recordcount before paged results
        total = s.GetRecordCount();

        s.Paged(pageindex, pagesize);
        s.OrderDesc("Hits");
        s.OrderDesc("Points");
        s.OrderDesc("NumberOfVotes");
        coll = s.ExecuteAsCollection<DAL.ItemCollection>();

Now the thing is, when my fid (FilterId) is larger then 0, the CategoryID filter does not work. It hits the breakpoint no problem but it's not getting filtered. Does it have something do do with the LIKE query? It works perfectly if i remove the if part and current s.where and change the query do this:

s.Where("CategoryID").IsEqualTo(fid);

Do I miss something important here?

Kind regards,
Mark

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

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

发布评论

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

评论(2

2024-08-28 14:23:39

看起来您正在查询:

SELECT * FROM Schema 
WHERE title LIKE ... 
   OR tags LIKE ... 
   AND isactive = true 
   AND categoryid = ...

但您想要的是:

SELECT * FROM Schema 
WHERE (title LIKE ... OR tags LIKE ...)
   AND isactive = true 
   AND categoryid = ...

您可以使用 AndExpression/OrExpression 语法后跟 CloseExpression 获得亚音速 2.1 中的括号。

It looks like you are querying:

SELECT * FROM Schema 
WHERE title LIKE ... 
   OR tags LIKE ... 
   AND isactive = true 
   AND categoryid = ...

but want you want is:

SELECT * FROM Schema 
WHERE (title LIKE ... OR tags LIKE ...)
   AND isactive = true 
   AND categoryid = ...

You can get brackets in subsonic 2.1 with the AndExpression/OrExpression syntax follwed by CloseExpression.

埖埖迣鎅 2024-08-28 14:23:39

自从我使用 2.1 以来已经有一段时间了,但我希望以下内容能够工作:

    DAL.ItemCollection coll = new DAL.ItemCollection();
    SubSonic.Select s = new SubSonic.Select();
    s.From(DAL.Item.Schema)
        .Where("Title").Like("%" + q + "%")
        .Or("Tags").Like("%" + q + "%")
        .And("IsActive").IsEqualTo(true);

    if (fid > 0)
    {
        s = s.And("CategoryID").IsEqualTo(fid);
        Session["TotalSearchResults"] = null;
    }
    s = s.Top(maxitems);

    //We'll get the recordcount before paged results
    total = s.GetRecordCount();

    s = s.Paged(pageindex, pagesize);
        .OrderDesc("Hits");
        .OrderDesc("Points");
        .OrderDesc("NumberOfVotes");
    coll = s.ExecuteAsCollection<DAL.ItemCollection>();

It's been a while since I've used 2.1 but I would expect the following to work:

    DAL.ItemCollection coll = new DAL.ItemCollection();
    SubSonic.Select s = new SubSonic.Select();
    s.From(DAL.Item.Schema)
        .Where("Title").Like("%" + q + "%")
        .Or("Tags").Like("%" + q + "%")
        .And("IsActive").IsEqualTo(true);

    if (fid > 0)
    {
        s = s.And("CategoryID").IsEqualTo(fid);
        Session["TotalSearchResults"] = null;
    }
    s = s.Top(maxitems);

    //We'll get the recordcount before paged results
    total = s.GetRecordCount();

    s = s.Paged(pageindex, pagesize);
        .OrderDesc("Hits");
        .OrderDesc("Points");
        .OrderDesc("NumberOfVotes");
    coll = s.ExecuteAsCollection<DAL.ItemCollection>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文