如何定义一个linq的泛型方法?

大刘0417 2017-03-06 01:08:21
程序中使用EF,
通过递归为easyUI的Tree加载节点:我想把网上的代码抽象成通用的(通过泛型、委托,任何数据类都可以根据自定的条件进行数据树型显示),相应的地方该如何更改?
DataTreeModel 应该是不变的,变化的是DataTable(项目中使用Entity Framework)参数和具体的数据提取条件?不会处理了,求教!

网上给的代码:

private List<JsonTree> CreateChildTree(DataTable dt, JsonTree jt)
{
int keyid = jt.id; //根节点ID
List<JsonTree> nodeList = new List<JsonTree>();
DataRow[] children = dt.Select("Parentid='" + keyid + "'");
foreach (DataRow dr in children)
{
JsonTree node = new JsonTree();
node.id = int.Parse(dr["id"].ToString());
node.text = dr["cityname"].ToString();
node.state = dr["state"].ToString();
node.attributes = CreateUrl(dt, node);
node.children = CreateChildTree(dt, node);
nodeList.Add(node);
}
return nodeList;
}


自己改的:
whereLambda参数(委托)根据不确定的映射表和不同的条件返回一个集合
private List<DataTreeModel> CreateChildrenNode<T>(Func<?, ?> whereLambda, DataTreeModel dtm)
{
Dictionary<string, string> attrDic = new Dictionary<string, string>();
List<DataTreeModel> nodeList = new List<DataTreeModel>(); //这是一个tree的数据模型
var children =??? //
foreach (DataRow dr in children)
{
JsonTree node = new JsonTree();
node.id = int.Parse(dr["id"].ToString());
node.text = dr["cityname"].ToString();
node.state = dr["state"].ToString();
node.attributes = CreateUrl(dt, node);
node.children = CreateChildTree(dt, node);
nodeList.Add(node);
}
37 return nodeList;
}

DataTreeModel的定义:

public class DataTreeModel
{
public int id { get; set; }
public string text { get; set; }
public string state { get; set; }
public Dictionary<string, string> attribute { get; set; }
public object children { get; set; }
}


我也不知道说明白没?
...全文
198 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
threenewbee 2017-03-06
  • 打赏
  • 举报
回复
引用 2 楼 dongxinxi 的回复:
你找到扩展方法的类 namespace System.Linq { public static class Enumerable { public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); } } 就会看到看到类似的方法
这就是正解
Poopaye 2017-03-06
  • 打赏
  • 举报
回复
看不懂啊,直接用Where不行吗?
  • 打赏
  • 举报
回复
你找到扩展方法的类 namespace System.Linq { public static class Enumerable { public static TSource First<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate); } } 就会看到看到类似的方法
  • 打赏
  • 举报
回复
http://bbs.csdn.net/topics/391015868#post-399091425 不是传递whereLambda,传递的得是source集合

111,119

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧