0

我想创建递归类别。使用连接时不加载任何项目。我不想外连接,因为如果一个类别没有项目,就不要显示。我该如何解决这个问题?

public string CategoryTree(int depth)
{
    StringBuilder sBuilder;   
    using (DataContext db = new DataContext())
    {
        var query = from n in db.Categories join m in db.Products on n.CatId equals m.CategoryId where n.ParentId == depth select new{n, m};
        sBuilder = new StringBuilder("<ul>");
        foreach (var q in query)
        {
            sBuilder.Append("<li>");
            sBuilder.Append("<a href='/Product/"+q.m.ProdId+">'"+q.n.Title+"</a>"+CategoryTree(q.n.CatId));
            sBuilder.Append("</li>");
        }
        sBuilder.Append("</ul>");
    }
    return sBuilder.ToString();
}

Categories
+---------+---------------+------------+
| CatId   | Title         | ParentId   |
+---------+---------------+------------+
|    1    | Electronic    |   NULL     |
+---------+---------------+------------+
|    2    | Computer      |   NULL     |
+---------+---------------+------------+
|    3    | Television    |     1      |
+---------+---------------+------------+
|    4    | Led           |     3      |
+---------+---------------+------------+
|    5    | Printer       |     2      |
+---------+---------------+------------+
|    6    | Laser Printer |     5      |
+---------+---------------+------------+

Products
+---------+---------------+------------+
| ProdId  | ProductName   | CategoryId |
+---------+---------------+------------+
|    1    | Samsung LED   |      4     |
+---------+---------------+------------+
|    2    | Sony LED TV   |      4     |
+---------+---------------+------------+    
4

1 回答 1

0

我将您的代码直接翻译成更可行的递归函数。我将它分成两部分以避免实例化多个DataContext类(这可能非常慢)。而且我还使用 Linq to XML 来使代码编写起来更安全。

public string CategoryTree(int? parentId)
{
    using (DataContext db = new DataContext())
    {
        return CategoryTree(db, parentId).ToString();
    }
}

private XElement CategoryTree(DataContext db, int? parentId)
{
    return new XElement(
        "ul", 
        from n in db.Categories
        join m in db.Products on n.CatId equals m.CategoryId
        where n.ParentId == parentId
        select new XElement(
            "li",
            new XElement(
                "a",
                new XAttribute(
                    "href",
                    "/Product/" + m.ProdId),
                n.Title),
            CategoryTree(db, n.CatId)));
}

现在,当然,这不起作用,因为parentId等于null没有返回任何元素,因为您的数据在该级别没有任何产品,因此连接不返回任何内容。在获取产品之前,您必须构建您的类别树。

所以这就是你真正需要做的:

private XElement CategoryTree(DataContext db, int? parentId)
{
    return new XElement(
        "ul", 
        from n in db.Categories
        where n.ParentId == parentId
        select new XElement(
            "li",
            new XElement(
                "span",
                n.Title),
            from m in db.Products
            where m.CategoryId == n.CatId
            select new XElement(
                "div",
                new XElement(
                    "a",
                    new XAttribute(
                        "href",
                        "/Product/" + m.ProdId),
                    m.ProductName)),
            CategoryTree(db, n.CatId)));
}

现在构建了以下树:

<ul>
  <li>
    <span>Electronic</span>
    <ul>
      <li>
        <span>Television</span>
        <ul>
          <li>
            <span>Led</span>
            <div>
              <a href="/Product/1">Samsung LED</a>
            </div>
            <div>
              <a href="/Product/2">Sony LED TV</a>
            </div>
            <ul />
          </li>
        </ul>
      </li>
    </ul>
  </li>
  <li>
    <span>Computer</span>
    <ul>
      <li>
        <span>Printer</span>
        <ul>
          <li>
            <span>Laser Printer</span>
            <ul />
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

但是您不想要任何不包含产品的类别。但是我们不知道在我们构建树时哪个不这样做,所以我们必须在之后修剪。

这是我进行修剪的方法:

public string CategoryTree(int? parentId)
{
    using (DataContext db = new DataContext())
    {
        var tree = CategoryTree(db, parentId);
        Action<string> prune = tag =>
        {
            foreach (var empty in tree
                .Descendants(tag)
                .Where(ul => !ul.Descendants("a").Any())
                .ToArray())
            {
                empty.Remove();
            }
        };
        prune("ul");
        prune("li");
        return tree.ToString();
    }
}

我首先修剪无产品ul标签,但这仍然会留下无产品li标签,所以我也必须修剪它们。

这是最终结果:

<ul>
  <li>
    <span>Electronic</span>
    <ul>
      <li>
        <span>Television</span>
        <ul>
          <li>
            <span>Led</span>
            <div>
              <a href="/Product/1">Samsung LED</a>
            </div>
            <div>
              <a href="/Product/2">Sony LED TV</a>
            </div>
          </li>
        </ul>
      </li>
    </ul>
  </li>
</ul>

我希望这就是你要找的。

于 2013-11-03T23:52:27.500 回答