I need some help with syntactic sugar. I have a ThisClass[3] and ThatClass[3].
我需要一些语法糖的帮助。我有一个ThisClass [3]和ThatClass [3]。
public class ThisClass
{
public string Thing1;
public string Thing2;
public string Thing3;
public string Thing4;
}
public class ThatClass
{
public string Thing1;
public string Thing2;
}
Each instance in the array of ThatClass was created based on an instance in the same position of array ThisClass. So ThatClass[0] has its fields with the same values as ThisClass[0], except it only has 2 fields instead of 4.
ThatClass数组中的每个实例都是基于数组ThisClass的相同位置的实例创建的。所以ThatClass [0]的字段与ThisClass [0]具有相同的值,除了它只有2个字段而不是4个字段。
I would like to now update each instance in the ThisClass array, with fields from the matching index position of the object in the ThatClass array. I could do nested for loops, but I need help thinking through a LINQ option.
我现在想更新ThisClass数组中的每个实例,其中包含来自ThatClass数组中对象的匹配索引位置的字段。我可以做嵌套for循环,但我需要帮助思考LINQ选项。
ThisClass[0].Thing1 = ThatClass[0].Thing1;
ThisClass[0].Thing2 = ThatClass[0].Thing2;
works but I am sure could be done better. Using C#, .NET 4.5.
有效,但我相信可以做得更好。使用C#,.NET 4.5。
3 个解决方案
#1
7
I don't see any need for nested loops:
我没有看到任何嵌套循环的需要:
for (int i = 0; i < theseClasses.Length; i++)
{
theseClasses[i].Thing1 = thoseClasses[i].Thing1;
theseClasses[i].Thing2 = thoseClasses[i].Thing2;
}
You could potentially add a CopyFrom(ThatClass)
method to ThisClass
, leading to:
您可以将CopyFrom(ThatClass)方法添加到ThisClass,从而导致:
for (int i = 0; i < theseClasses.Length; i++)
{
theseClasses[i].CopyFrom(thoseClasses[i]);
}
... but that's all I'd do. LINQ is do to with querying, not causing side-effects... I don't think it's a good fit here.
......但这就是我要做的。 LINQ可以查询,不会造成副作用......我认为这不适合这里。
#2
1
Attention: As @Jon put, LINQ is not about causing side-effects and if you do so you may end up with a code with unexpected behavior (but it's possible).
注意:正如@Jon所说,LINQ不是要引起副作用,如果你这样做,你可能会得到一个意外行为的代码(但这是可能的)。
This code does that:
这段代码可以做到:
ThisClass[] these = new ThisClass[100];
ThatClass[] those = new ThatClass[100];
// init these and those items
those.Zip(these, (that, @this) =>
{
@this.Thing1 = that.Thing1;
@this.Thing2 = that.Thing2;
return that;
}).ToList();
#3
-1
As you're asking for LINQ... this will get you an unrelated IEnumerable<ThisClass>,
and will not modify the original array. (I'm assuming that the thisClass
and thatClass
arrays are called thisArray
and thatArray
, respectively)
当你要求LINQ时...这将获得一个不相关的IEnumerable
thisArray.Select((n, x) => { n.Thing1 = thatArray[x].Thing1; n.Thing2 = thatArray[x].Thing2; return n; }).ToArray();
(If you really wanted LINQ and assigning it, just assign it back to the original array)
(如果你真的想要LINQ并分配它,只需将它分配回原始数组)
#1
7
I don't see any need for nested loops:
我没有看到任何嵌套循环的需要:
for (int i = 0; i < theseClasses.Length; i++)
{
theseClasses[i].Thing1 = thoseClasses[i].Thing1;
theseClasses[i].Thing2 = thoseClasses[i].Thing2;
}
You could potentially add a CopyFrom(ThatClass)
method to ThisClass
, leading to:
您可以将CopyFrom(ThatClass)方法添加到ThisClass,从而导致:
for (int i = 0; i < theseClasses.Length; i++)
{
theseClasses[i].CopyFrom(thoseClasses[i]);
}
... but that's all I'd do. LINQ is do to with querying, not causing side-effects... I don't think it's a good fit here.
......但这就是我要做的。 LINQ可以查询,不会造成副作用......我认为这不适合这里。
#2
1
Attention: As @Jon put, LINQ is not about causing side-effects and if you do so you may end up with a code with unexpected behavior (but it's possible).
注意:正如@Jon所说,LINQ不是要引起副作用,如果你这样做,你可能会得到一个意外行为的代码(但这是可能的)。
This code does that:
这段代码可以做到:
ThisClass[] these = new ThisClass[100];
ThatClass[] those = new ThatClass[100];
// init these and those items
those.Zip(these, (that, @this) =>
{
@this.Thing1 = that.Thing1;
@this.Thing2 = that.Thing2;
return that;
}).ToList();
#3
-1
As you're asking for LINQ... this will get you an unrelated IEnumerable<ThisClass>,
and will not modify the original array. (I'm assuming that the thisClass
and thatClass
arrays are called thisArray
and thatArray
, respectively)
当你要求LINQ时...这将获得一个不相关的IEnumerable
thisArray.Select((n, x) => { n.Thing1 = thatArray[x].Thing1; n.Thing2 = thatArray[x].Thing2; return n; }).ToArray();
(If you really wanted LINQ and assigning it, just assign it back to the original array)
(如果你真的想要LINQ并分配它,只需将它分配回原始数组)