这是我第一次听说LINQ
,我对此一无所知。请对我温柔一点。
我有这组数据。
+---------+--------+---------------+
| RadioID | NodeID | SourceRadioID |
+---------+--------+---------------+
| R0 | 1 | |
| R1 | 1 | |
| R2 | 1 | |
| R3 | 1 | |
| R4 | 1 | |
| R5 | 2 | |
| R6 | 2 | |
| R7 | 2 | R0 |
| R8 | 2 | |
| R9 | 2 | |
| R10 | 11 | |
| R11 | 11 | R9 |
| R12 | 11 | |
| R13 | 11 | |
+---------+--------+---------------+
我需要做的是编写一个返回列表的方法NodeID
。例子,
List<int> dependentNode = GetChildNode(1); // int ParentNode
我的预期结果是 NodeIDs: 2 and 11
。
NodeID = 2
被包括在内,因为有一个RadioID = R7
连接到RadioID = R0
的属于NodeID = 1
。
NodeID = 11
也包括在内,因为RadioID = R11
连接到Radio = R9
哪个属于NodeID = 2
(也连接到NodeID = 1
)。
我查找这篇文章,但我总是得到StackOverFlowException
这是完整的代码:
public class RadioEntity
{
public string RadioID { get; set; }
public int NodeID { get; set; }
public string SourceRadioID { get; set; }
}
public class SampleDemo
{
public void SampleMethod()
{
Func<int, int,List<int>> GetChildNode = null;
GetChildNode = (x, y) =>
{
return (from _x in GetRadio()
where (GetRadio().Where(i => i.NodeID == x).Select(i => i.RadioID)).Contains(_x.RadioID)
from _y in new[] { _x.NodeID }.Union(GetChildNode(_x.NodeID, y + 1))
select _y).ToList<int>();
};
var _res = GetChildNode(1, 0);
}
public List<RadioEntity> GetRadio()
{
List<RadioEntity> _returnVal = new List<RadioEntity>();
_returnVal.Add(new RadioEntity() { RadioID = "R0", NodeID = 1, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R1", NodeID = 1, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R2", NodeID = 1, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R3", NodeID = 1, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R4", NodeID = 1, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R5", NodeID = 2, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R6", NodeID = 2, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R7", NodeID = 2, SourceRadioID = "R0" });
_returnVal.Add(new RadioEntity() { RadioID = "R8", NodeID = 2, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R9", NodeID = 2, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R10", NodeID = 11, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R11", NodeID = 11, SourceRadioID = "R9" });
_returnVal.Add(new RadioEntity() { RadioID = "R12", NodeID = 11, SourceRadioID = "" });
_returnVal.Add(new RadioEntity() { RadioID = "R13", NodeID = 11, SourceRadioID = "" });
return _returnVal;
}
}
您可以建议是否有更好的方法来做到这一点。在这里对不起新手。