Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何处理作为参数传递到方法-C#.NET 3.5中的Lambda表达式_C#_.net_Linq_Lambda - Fatal编程技术网

如何处理作为参数传递到方法-C#.NET 3.5中的Lambda表达式

如何处理作为参数传递到方法-C#.NET 3.5中的Lambda表达式,c#,.net,linq,lambda,C#,.net,Linq,Lambda,我对Lambda表达式的了解有点不稳定,虽然我可以编写使用Lambda表达式(aka LINQ)的代码,但我正在尝试编写自己的方法,该方法使用Lambda表达式类型的一些参数 背景:我正在尝试编写一个方法,该方法从任何其他对象类型返回TreeItem类型的对象的树集合。到目前为止,我有以下几点: public class TreeItem { public string Id { get; set; } public string Text { get; set; }

我对Lambda表达式的了解有点不稳定,虽然我可以编写使用Lambda表达式(aka LINQ)的代码,但我正在尝试编写自己的方法,该方法使用Lambda表达式类型的一些参数

背景:我正在尝试编写一个方法,该方法从任何其他对象类型返回TreeItem类型的对象的树集合。到目前为止,我有以下几点:

public class TreeItem
{
    public string Id { get; set; }
    public string Text { get; set; }

    public TreeItem Parent { get; protected set; }

    public IList<TreeItem> Children
    {
        get
        {
            // Implementation that returns custom TreeItemCollection type
        }
    }

    public static IList<TreeItem> GetTreeFromObject<T>(IList<T> items,
        Expression<Func<T, string>> id,
        Expression<Func<T, string>> text,
        Expression<Func<T, IList<T>>> childProperty) where T : class
    {
        foreach (T item in items)
        {
           // Errrm!?? What do I do now?
        }

        return null;
    }
}
公共类树项
{
公共字符串Id{get;set;}
公共字符串文本{get;set;}
公共树项父项{get;protected set;}
公营儿童
{
得到
{
//返回自定义TreeItemCollection类型的实现
}
}
公共静态IList GetTreeFromObject(IList项,
表达式id,
表达式文本,
表达式(属性),其中T:class
{
foreach(项目中的T项目)
{
//嗯!我现在该怎么办?
}
返回null;
}
}
…可通过以下方式调用

IList<TreeItem> treeItems = TreeItem.GetTreeFromObject<Category>(
    categories, c => c.Id, c => c.Name, c => c.ChildCategories);
IList treeItems=TreeItem.GetTreeFromObject(
类别,c=>c.Id,c=>c.Name,c=>c.ChildCategories);
我可以用字符串值替换表达式,只使用反射,但我试图避免这种情况,因为我想使其成为强类型

我这样做的原因是,我有一个控件,它接受类型TreeItem的列表,而我有几十种不同的类型,它们都在一个树状结构中,并且不想为每种类型编写单独的转换方法(尝试遵守DRY原则)

我这样做对吗?也许有更好的方法吗?

没有“lambda表达式”这样的类型。lambda表达式可以转换为兼容的委托类型,也可以转换为

您现有的方法签名使用表达式树,但还不清楚是否真的需要。尝试委托表单(仅更改一些参数名称):

公共静态IList GetTreeFromObject(IList项,
Func idSelector,
Func文本选择器,
Func childPropertySelector)其中T:class
然后你可以这样做:

foreach (T item in items)
{
    string id = idSelector(item);
    string text = textSelector(item);
    IList<T> children = childPropertySelector(item);
    // Do whatever you need here
}
foreach(项目中的T项)
{
字符串id=idSelector(项);
字符串文本=文本选择器(项目);
IList children=childPropertySelector(项目);
//你需要什么就做什么
}

太棒了!正是我想要的,非常感谢你!注意,表达式可以转换为委托,如下所示:Func childFunc=childProperty.Compile();但正如您所说,没有必要,只需传入Func委托类型即可,干杯!
foreach (T item in items)
{
    string id = idSelector(item);
    string text = textSelector(item);
    IList<T> children = childPropertySelector(item);
    // Do whatever you need here
}