使用递归类和内连接

时间:2013-10-31 14:56:18

标签: c# asp.net linq join recursion

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

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     |
+---------+---------------+------------+    

1 个答案:

答案 0 :(得分: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>

我希望这就是你要找的东西。