C# 将数据库转换为实体数据模型。事实上,它也会使删除变得容易,当你删除一个边缘时,它可以自动从每个节点删除它。非常感谢你的所有回复,这当然给了我一些思考的东西。我想我会沿着Hightechrider建议的路线走,直到遇到一些问题。因此,我接受了他的回答。 pu
C# 将数据库转换为实体数据模型。事实上,它也会使删除变得容易,当你删除一个边缘时,它可以自动从每个节点删除它。非常感谢你的所有回复,这当然给了我一些思考的东西。我想我会沿着Hightechrider建议的路线走,直到遇到一些问题。因此,我接受了他的回答。 pu,c#,list,reference,C#,List,Reference,将数据库转换为实体数据模型。事实上,它也会使删除变得容易,当你删除一个边缘时,它可以自动从每个节点删除它。非常感谢你的所有回复,这当然给了我一些思考的东西。我想我会沿着Hightechrider建议的路线走,直到遇到一些问题。因此,我接受了他的回答。 public class Node { // Has Some Data! } public class Branch { // Contains references to Nodes public Node NodeA
将数据库转换为实体数据模型。事实上,它也会使删除变得容易,当你删除一个边缘时,它可以自动从每个节点删除它。非常感谢你的所有回复,这当然给了我一些思考的东西。我想我会沿着Hightechrider建议的路线走,直到遇到一些问题。因此,我接受了他的回答。
public class Node
{
// Has Some Data!
}
public class Branch
{
// Contains references to Nodes
public Node NodeA
public Node NodeB
}
public class Graph
{
public List<Node> Nodes;
public List<Branch> Branches;
}
class Branch
{
public Branch(Node nodeA, Node nodeB) { NodeA = nodeA; NodeB = nodeB; }
public Node NodeA { get; set; }
public Node NodeB { get; set; }
}
class Node
{
public Node(string name) { Name = name; }
public string Name { get; set; }
}
List<Node> nodes = new List<Node>() { new Node("Apple"), new Node("Banana") };
List<Branch> branches = new List<Branch>() { new Branch(nodes[0], nodes[1]), new Branch(nodes[1], nodes[0]) };
Node node = nodes[0];
nodes.Remove(node);
var query = from branch in branches
where branch.NodeA == node || branch.NodeB == node
select branch;
foreach (Branch branch in query)
{
if (branch.NodeA == node)
branch.NodeA = null;
if (branch.NodeB == node) // could just be 'else' if NodeA cannot equal NodeB
branch.NodeB = null;
}
public class Node
{
// Has Some Data!
public List<Branch> BranchesIn;
public List<Branch> BranchesOut; // assuming this is a directed graph
public void Delete()
{
foreach (var branch in BranchesIn)
branch.NodeB.BranchesOut.Remove(branch);
foreach (var branch in BranchesOut)
branch.NodeA.BranchesIn.Remove(branch);
BranchesIn.Clear();
BranchesOut.Clear();
}
}
public class Branch
{
// Contains references to Nodes
public Node NodeA
public Node NodeB
}