C#中三元运算符的写法:三个操作数a1,a2,a3 表达式a1=a2>a3?a2:a3 意思为如果a2>a3,a1=a2,否则 a1=a3
列表的遍历,返回第一个遍历的元素:
/// <summary>
/// 返回当前栏目的路径,不超过三级
/// </summary>
/// <param name="yc"></param>
/// <param name="yclist"></param>
/// <returns></returns>
public string GetNodePath(Ym_Columns yc,List<Ym_Columns> yclist)
{
if (yc.fatherID == 0)
return yc.Id + suffix;
else
{
Ym_Columns parent1 = yclist.Find((o) => { return o.Id == yc.fatherID;}); //find方法为遍历列表yclist,返回第一个匹配的元素
if (parent1.fatherID != 0)
{
Ym_Columns parent2 = yclist.Find((o) => { return o.Id == parent1.fatherID; });
if (parent2.fatherID == 0)
{
return parent2.Id + suffix + "/" + parent1.Id + suffix + "/" + yc.Id + suffix;
}
else
{
return "路径有误";
}
}
else
return parent1.Id + suffix + "/" + yc.Id + suffix;
}
}
C#中三元运算符的写法以及列表的遍历