code/Athena0.0.1/common/Util/WebControl/QueryTree.cs
(开头部分) 3KB这里只显示每个文件的开头 60 行。登录后可以解锁完整代码。
using Athena.common.Util.Attributes;
using Athena.common.Util.Extension;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
namespace Athena.common.Util.WebControl
{
/// <summary>
/// 树形结构查询
/// </summary>
public static class QueryTree
{
/// <summary>
/// 树形查询条件
/// </summary>
/// <param name="entityList">数据源</param>
/// <param name="condition">查询条件</param>
/// <param name="primaryKey">主键</param>
/// <param name="parentId"></param>
/// <returns></returns>
public static List<T> TreeWhere<T>(this List<T> entityList, Predicate<T> condition, string primaryKey, string parentId = "ParentId") where T : class
{
List<T> locateList = entityList.FindAll(condition);
var parameter = Expression.Parameter(typeof(T), "t");
//模糊查询表达式
List<T> treeList = new List<T>();
foreach (T entity in locateList)
{
//先把自己加入进来
treeList.Add(entity);
//向上查询
string pId = entity.GetType().GetProperty(parentId).GetValue(entity, null).ToString();
while (true)
{
if (string.IsNullOrEmpty(pId) && pId == "0")
{
break;
}
Predicate<T> upLambda = (Expression.Equal(parameter.Property(primaryKey), Expression.Constant(pId))).ToLambda<Predicate<T>>(parameter).Compile();
T upRecord = entityList.Find(upLambda);
if (upRecord != null)
{
treeList.Add(upRecord);
pId = upRecord.GetType().GetProperty(parentId).GetValue(upRecord, null).ToString();
}
else
{
break;
}
}
}
return treeList.Distinct().ToList();
}
/// <summary>
/// 树形查询条件
后面还有 36 行代码,解锁后查看完整代码
24 小时内免费解锁 3 个项目,之后 1 积分/个。 规则说明
AI 解读
登录后可用,每次 10 积分,解读结果公开显示在下面。
还没有人解读过这个文件。
