简体   繁体   English

需要将一段PHP代码转换为C#

[英]Need to convert piece of PHP code to C#

I need to convert two PhP functions (sfGetCategoryAllTree and sfSideTree) to C#. 我需要将两个PhP函数(sfGetCategoryAllTree和sfSideTree)转换为C#。 I'm not used to PhP at all. 我一点都不习惯PhP。 I will show you the functions and what I did by now. 我将向您展示功能以及我现在所做的。

/**
 * Retrieves user data given a user id or user object.
 *
 * @param string $output Optional, default is Object. Either OBJECT, ARRAY_A, or ARRAY_N.
 * @return Category|null Category on success or null on failure
 */
function sfGetCategoryAllTree($output = OBJECT,$id='') {

    global $sfdb, $table_category, $table_category_lang;

    $categories = $sfdb->run("SELECT * FROM $table_category c"'
        ORDER BY position ASC", '', true)
            ->fetchAll($output);

    $all_categories = array();
    $all_relation = array();
    $tree = array();
    foreach ($categories as $c) {

        $all_categories[$c['id']] = $c;
        if ($c['parent_id'] == null) {
            $tree[] = $c;
            $all_relation[0][] = $c['id'];
        } else {
            $all_relation[$c['parent_id']][] = $c['id'];
        }
    }

    if (isset($all_relation[0])) {
        $tree = sfSideTree($all_relation[0], $all_categories, $all_relation);
    }
    return ($tree) ? $tree : array();
}

/**
 * Helps create the tree
 *
 * @param array $branch
 * @param array $categories
 * @param array $relation
 * @return array
 */
function sfSideTree($branch, $categories, $relation) {
    $tree = array();
    if (!empty($branch)) {
        foreach ($branch as $b) {
            $aux = $categories[$b];
            if (isset($relation[$b]) && is_array($relation[$b])) {
                $aux['categories'] = sfSideTree($relation[$b], $categories, $relation);
            } else {
                $aux['categories'] = array();
            }
            $tree[] = $aux;
        }
    }
    return $tree;
}

Here what I did so far: 这是我到目前为止所做的:

public void sfSideTree(int[] branch, Category[] categories, int[] relation)
        {
            Category[] tree = new Category[10];

            foreach (var b in branch)
            {
                var aux = categories[b];
                if (relation[b] != null)
                {
                    aux[]....
                }
            }
        }

        public void sfGetCategoryAllTree()
        {
            var categories = this.Query().Select().ToList();

            /*
            $all_categories = array();
            $all_relation = array();
            $tree = array();
            */
            //IList<Category> all_categories = new List<Category>();
            //IList<Category> all_relation = new List<Category>();
            //IList<Category> tree = new List<Category>();
            Category[] all_categories = new Category[10];
            int[] all_relation = new int[10];
            Category[] tree = new Category[10];


            foreach (var c in categories)
            {
                //$all_categories[$c['id']] = $c;
                all_categories[c.Id] = c;
                //all_categories.Add(new Category() { Id = item.Id });
                if (c.ParentId == 0)
                {
                    tree[??] = c; //???
                    all_relation[0] = c.Id;
                    /*

                    $tree[] = $c;
                    $all_relation[0][] = $c['id'];*/
                }
                else
                {
                    all_relation[c.ParentId] = c.Id;
                    //$all_relation[$c['parent_id']][] = $c['id'];
                }
            }

            if (all_relation != null)
            {
                sfSideTree(all_relation[0], all_categories, all_relation);
            }

                return tree;
            /*return ($tree) ? $tree: array();*/
        }

PhP arrays make life harder to translate. PhP阵列使生活更难翻译。 Does someone can help me? 有人可以帮我吗?

David 大卫

Edit 编辑

To answer to OwlSolo here the structure of Category class 在这里回答OwlSolo类别类的结构

public class Category
    {
        public int Id { get; set; }
        public int ParentId { get; set; }
        public int Position { get; set; }
        public string Title { get; set; }
        public string Description { get; set; }
        public bool Enabled { get; set; }
    }

PHP arrays are not really 100% compatible to C# types, but you can get around that by using a lot of Dictionary for instance, as PHP lets you address field types by both int or string equivalents. PHP数组实际上不是100%与C#类型兼容,但是您可以通过使用很多Dictionary(例如,Dictionary)来解决此问题,因为PHP允许您使用int或等效字符串来寻址字段类型。 This is certainly not the best practice, but so far is it rather unclear what data youre actually storing. 当然,这不是最佳实践,但是到目前为止,您还不清楚您实际上存储了什么数据。 I gave it a shot, but it probably wont work right out of the box . 我试了一下,但它可能开箱即用 Depending on what data it actually is you have, some adjustments might be needed. 根据您实际拥有的数据,可能需要进行一些调整。

EDIT: In order for this to become a tree, your Category class also need a way to reference its children - in PHP this is accomplished by the ["categories"] field. 编辑:为了使它成为树,您的Category类还需要一种引用其子级的方法-在PHP中,这是通过[“ categories”]字段完成的。

class Category
{
    [...]
    IList<Category> children;
}


//For instance as global arrays...
int[] all_categories;
int[] all_relation;



List<Category>  sfGetCategoryAllTree(output, id)
{   
    List<Category> tree;
    //$categories = $sfdb->run("SELECT * FROM $table_category c"'
    //    ORDER BY position ASC", '', true)
    //        ->fetchAll($output);  
    List<Category> categories = this.Query().Select().ToList();// Assuming this is aquivalent to the above selection statement...

    foreach (Category c in categories)
    {       
        all_categories[c.Id]  
        if (c.ParentId != 0) // The is no NULL for int, you might want to use the nullable type "int?" or an additional bool isRoot
        {
            if (tree == null) tree = new List<Category>();
            tree.Add(c);
            all_relation[0] = c.Id; 
        }else
        {
            all_relation[c.ParentId] = c.Id;
        }
    }   
    if (all_relation[0] != 0)
    {
        tree = sfSideTree(all_relation[0], all_categories, all_relation);
    }

    return tree;//returns the tree, or NULL 
}

List<Category> sfSideTree (int[] branch, Dictionary<int, Category> categories, int[] relation)
{
    List<Category> tree = new List<Category>();
    if (branch != null)
    {
        foreach (var b in branch)
        {
            if (relation[b] != null)
            {

                categories[b].categories = sdSideTree(relation[b], categories, relation)                        
            } else
            {
                categories[b].categories = new List<Category>();
            }
            tree = aux;
        }
    }
    return tree;
}

Coming from your comment on my last answer, this is how I would implement this in C#: 来自您对我的最后一个答案的评论,这就是我将如何在C#中实现的方法:

public void makeTree()
{
    //Make a Dictionary with the ID als key
    Dictionary<int, Category> categories = this.Query().Select().ToDictionary(a => a.ID, a => a);

    foreach (Category c in categories)
    {
        if (c.ParentID != 0)
        {
            if (categories[c.ParentID].children == null)
            {
                categories[c.ParentID].children = new List<Category>();
            }
            categories[c.ParentID].children.add(c);         
        }//ParentID == 0 means its a root element
    }
}

This simply creates a tree structure from you query. 这只是根据您的查询创建树结构。 If you need to expand a Category just access its list of children. 如果您需要扩展类别,只需访问其子级列表即可。 Unless I'm overlooking something, this is what you really want to do here. 除非我忽略了某些内容,否则这就是您真正想要在此处执行的操作。 There is no need to keep separate lists or another cluttery method for traversing the tree. 遍历树时无需保留单独的列表或其他混乱的方法。

If you want to list all root Categories just go: 如果要列出所有根类别,请执行以下操作:

public List<Category> getRootNodes()
{
   List<Category> rootNodes = new List<Category>();
   foreach (Category c in categories)
   {
       if (c.ParentID == 0) rootNodes.Add(c);
   }
   return rootNodes;
}

EDIT Just realized that last function is pretty uneccessary as you can do it with linq much more easily 编辑刚刚意识到最后一个功能是不必要的,因为您可以使用linq轻松得多

rootNodes = catergories.Where(x => x.ParentID == 0);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM