Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.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
使用LINQ在一行中的一维实例数组上应用操作_Linq_Generics - Fatal编程技术网

使用LINQ在一行中的一维实例数组上应用操作

使用LINQ在一行中的一维实例数组上应用操作,linq,generics,Linq,Generics,考虑以下结构: internal struct Coordinate { public Double Top { get; set; } public Double Left { get; set; } } internal struct Dimension { public Double Height { get; set; } public Double Width { get; set; } } internal struct Property {

考虑以下结构:

internal struct Coordinate
{
    public Double Top { get; set; }
    public Double Left { get; set; }
}

internal struct Dimension
{
    public Double Height { get; set; }
    public Double Width { get; set; }
}

internal struct Property
{
    public Boolean Visible { get; set; }
    internal String Label { get; set; }
    public String Value { get; set; }
    internal Coordinate Position { get; set; }
    public Dimension Dimensions { get; set; }
}
我需要操纵20个左右的属性实例。我希望这样做尽可能干净。。。是否可以在一行代码中对属性数组应用多个操作

我的想法大致如下:

new []
{
    InstanceOfProperty,
    InstanceOfProperty,
    InstanceOfProperty ...
}.Each(p => p.Dimensions.Height = 100.0);

如果您编写了自己的
每个
方法,并执行
操作
委托,则可以

编辑:

事实上它不起作用。您正在使用值类型。有什么原因不能是


Dimension
Property
都必须是该赋值的引用类型,才能在处理列表后反映出来。

您可以为每个扩展方法编写一个
,但由于您的对象是结构,因此无法使其在
IEnumerable
上工作。但是,您可以使用带有ref参数的委托创建一个在数组上工作的扩展方法:

public static class ExtensionMethods
{
    public delegate void RefAction<T>(ref T arg);

    public static void Each<T>(this T[] array, RefAction<T> action)
    {
        for(int i = 0; i < array.Length; i++)
        {
            action(ref array[i]);
        }
    }
}

...

new []
{
    InstanceOfProperty,
    InstanceOfProperty,
    InstanceOfProperty ...
}.Each((ref Property p) => p.Dimensions.Height = 100.0);
总的来说,如果您的类型是类而不是结构,那么一切都会简单得多。

我最后做了以下工作:

new List<Property> { InstanceOfProperty, InstanceOfProperty, ... }.ForEach((p =>
{
    p.Dimensions.Height = 0.0;
}));
newlist{InstanceOfProperty,InstanceOfProperty,…}.ForEach((p=>
{
p、 尺寸。高度=0.0;
}));

注意:我已将结构转换为类。如果我使用原始的基于结构的设计;为了改变它们的价值,我不得不“更新”这些结构。像这样:

newlist{InstanceOfProperty,InstanceOfProperty,…}.ForEach((p=>
{
p、 尺寸=新尺寸{高度=0.0};
}));

看起来很整洁。这将是我第二次“尝试”。谢谢你能检查一下我对这个问题的回答,并告诉我你是否预见到任何问题吗?
new List<Property> { InstanceOfProperty, InstanceOfProperty, ... }.ForEach((p =>
{
    p.Dimensions.Height = 0.0;
}));
new List<Property> { InstanceOfProperty, InstanceOfProperty, ... }.ForEach((p =>
{
    p.Dimensions = new Dimension { Height = 0.0 };
}));