0

I need some help with syntactic sugar. I have a ThisClass[3] and 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.

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[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.

3 Answers 3

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:

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.

Sign up to request clarification or add additional context in comments.

Comments

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).

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();

Comments

-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)

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)

3 Comments

@Downvoter, What. Is. The. Problem? He asked for LINQ, I gave LINQ. I DID mention it would not modify the original one, and it DOES work. I've tested it. What is the problem?
Your code won't work, because Select() is lazy. So, if you do this, nothing will actually happen. That's one reason why using LINQ to do this is a bad idea.
@svick Fixed that, my overlook.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.