亚音速2.1:我疯了吗?

时间:2010-02-13 14:38:44

标签: c# sql subsonic

我正在使用亚音速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);

我是否想念一些重要的事情?

亲切的问候, 标记

2 个答案:

答案 0 :(得分:2)

看起来你在查询:

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中的括号。

答案 1 :(得分:0)

自从我使用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>();