5
這是我第一次聽到LINQ
,我不知道它。請對我溫柔。使用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
我的預期結果是NodeID: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;
}
}
,如果有更好的方法來做到這一點你可以建議。對不起,新手在這裏。
這是怎麼得到兒童的孩子嗎? – 2013-05-01 03:23:50
這個傢伙爲了更好的表現如何投票呢? – 2013-05-01 03:31:18
只是放鬆,downvotes不是一個人的攻擊,他們並不意味着除了給出的答案是不正確的,並沒有回答這個問題。 – 2013-05-01 03:39:00