sql-server – 在实体框架4中有没有实用的方法来使用hierarchyID
|
现在,包含HierarchyID的CLR UDT在实体框架4中不受支持.HierarchyID.ToString()是有用的,但一旦任何项目有10个兄弟节点(基本结构是/ 3/4/12 /或/ 3/4/2 /所以第12个节点将在第2个节点之前排序).
更多关于潜在的选择: 带回一个varbinary的hierarchyID,并实现自己的二进制排序 所以我的问题是有人围绕这个限制吗?您是否使用上述任何策略?如果是这样,怎么办? 解决方法嗯,我似乎正在获得意见,但没有回应.我有一些立即需要处理SQL之上的层次结构,所以我把一个静态帮助类放在一起.我不认为这是一个完整的解决方案,但到目前为止,它的工作相对较好. PadPath在这里确实是关键的功能.public static class SQLHierarchyManipulatin {
const int DEFAULT_PAD_LEN = 3;
const char DEFAULT_PAD_CHAR = '0';
public static string PadPath(string Hierarchy) {
return PadPath (Hierarchy,DEFAULT_PAD_LEN);
}
public static string PadPath(string Hierarchy,int padLen) {
string[] components = Hierarchy.Split('/');
for (var i = 0; i < components.Length; i++ ) {
if (components[i] != "") {
components[i] = components[i].PadLeft(padLen,DEFAULT_PAD_CHAR);
}
}
return string.Join("/",components);
}
public static int CurrentNodeIndex(string Hierarchy) {
string[] components = Hierarchy.Split('/');
string startItem = components[components.Length - 2]; //one slot back from trailing slash
return int.Parse(startItem);
}
public static string ParentPath (string Hierarchy) {
return Hierarchy.Substring(0,Hierarchy.TrimEnd('/').LastIndexOf('/') + 1);
}
public static string AppendChildWithPadding (string Hierarchy,int childIndex,int padLen) {
return AppendChild(Hierarchy,childIndex,DEFAULT_PAD_LEN);
}
public static string AppendChildWithPadding (string Hierarchy,int childIndex) {
return AppendChild(Hierarchy,DEFAULT_PAD_LEN);
}
public static string AppendChild (string Hierarchy,int padLen) {
return Hierarchy + childIndex.ToString().PadLeft(padLen,DEFAULT_PAD_CHAR) + "/";
}
}
希望这有助于某人!虽然,我还是想听听人的意见. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
