工作中经常用到的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(); } }
时间: 2024-10-18 10:45:45