工作中经经常使用到的linq依据传入数据集合查询相应子级数据,整理共享,希望大家都能用得上,代码中doublesArray 为父节点相应ID数据集合,再依据ID数据集合查询所有子级数据。
//获取缓存数据 object obj = Caching.GetCache(CacheKey + UModel.RoleId); if (obj != null)//判读缓存数据是否null { SysFunList = (DataSet)obj; } else { string strSql = "select * from View_SysRoleList where LoginID='" + UModel.LoginId + "' order by [Custom_Displayorder]"; SysFunList = SqlHelper.Query(SqlHelper.LocalSqlServer, strSql.ToString()); Caching.SetCache(CacheKey + UModel.RoleId, SysFunList); } //linq获取数据集内全部父节点数据 var query = (from t in SysFunList.Tables[0].AsEnumerable() where t.Field<int>("Custom_ParentNodeId") == 0 select t); if (query.Count() > 0) { //linq获取数据内全部父节点相应ID var doublesArray = query.Select(one => one.Field<int>("Custom_NodeId")); if (doublesArray.Count() > 0) { //linq 依据ID数据结合获取相应下一级数据 var foos = from tq in SysFunList.Tables[0].AsEnumerable() where doublesArray.Contains(tq.Field<int>("Custom_ParentNodeId")) orderby tq.Field<int>("Custom_Displayorder") descending select tq; //转换为DataTable DataTable boundTable = foos.CopyToDataTable<DataRow>(); RepeaterMeau.DataSource = boundTable; RepeaterMeau.DataBind(); } }