递归的实际应用

IList<CBF.Model.BOMDisplay> list = new List<CBF.Model.BOMDisplay>();

protected override bool CallRead()
{
CBF.Model.BOM bom = this.Operation.GetBOMById(CHelper.GetQueryString(CString.ID).ToInt());

if (!bom.Id.IsNull())
{
list.Add(this.Operation.BOMToBOMDisplay(bom, list, 0));

//this.Operation.AddEq("ParentId", bom.Id.ToString());
IList<CBF.Model.BOM> boms = this.Operation.GetRootBOM(bom.Id.ToString());

this.Operation.ShowBOM(boms, list, 1);

treeList.DataSource = list;
treeList.DataBind();
//ExtAspNet.TreeNode node = new ExtAspNet.TreeNode();
//node.Text = bom.ItemNO;
//tr_BOM.Nodes.Add(node);
//ShowBOM(bom, node.Nodes);
}

;

return base.InitPage();
}

public void ShowBOM(IList<CBF.Model.BOM> boms, IList<CBF.Model.BOMDisplay> listBoms,int parentId)
{
foreach (var b in boms)
{
BOMDisplay bOMToBOMDisplay = BOMToBOMDisplay(b, listBoms,parentId);
listBoms.Add(bOMToBOMDisplay);

//ICriteria criteria = this._session.CreateCriteria(typeof(CBF.Model.BOM));

//criteria.Add(Expression.In("ParentId", new List<string>() { b.Id.ToString() }));
string strSQL = string.Format(@"if exists(select * from mes_bom where parentid ='{0}')
begin
select * from mes_bom where parentid ='{1}'
end
else if exists(select * from MES_BOM where ItemNO = '{2}' and ParentID = 0)
begin
select * from MES_BOM where ParentID in(select ID from MES_BOM where ItemNO = '{3}' and ParentID = 0)
end
else
select * from mes_bom where parentid ='{4}'

", b.Id, b.Id, b.ItemNO, b.ItemNO,b.Id);

IList<CBF.Model.BOM> iboms = _session.CreateSQLQuery(strSQL).AddEntity(typeof(BOM)).List<BOM>();

if (iboms.Count > 0)
{
ShowBOM(iboms, listBoms, bOMToBOMDisplay.IId);
}

}
}

public BOMDisplay BOMToBOMDisplay(BOM bom, IList<BOMDisplay> boms,int parentId)
{
BOMDisplay bomDis = new BOMDisplay();
bomDis.IId = boms.Count + 1;
bomDis.Id = bom.Id;
bomDis.ItemNO = bom.ItemNO;
bomDis.ItemName = bom.ItemName;
bomDis.Unit = bom.Unit;
bomDis.Qty = bom.Qty;
bomDis.Yield = bom.Yield;
bomDis.Scrap = bom.Scrap;
bomDis.ParentId = parentId;

return bomDis;
}

原文地址:https://www.cnblogs.com/chengjun/p/5378604.html