3

I have 2D matrix in C# which is built a little complicated. This is the code of matrix generation:

    string[][] learningInputNotCodified = learningDataRaw.Select(ldr => new string[] {
            ldr.ChangeType.ToString(),
            ldr.MutationOperator.Name??"null",
            ldr.MutationOperator.Before??"null",
            ldr.MutationOperator.After??"null",
            ldr.SLOC.ToString(),
            dBContext.TestCases
                .Count(tc => tc.UserMutantPlay.MutantId == ldr.Id).ToString(),
            dBContext.TestCases
                .Where(tc => tc.UserMutantPlay.MutantId == ldr.Id)
                .Select(rtc => new {
                    killCount = dBContext.TestCases
                        .Where(tc => tc.IsMutantKilled
                        && tc.UserMutantPlay.MutantId == ldr.Id
                        && !tc.InputValues.Any(iv =>
                        rtc.InputValues.FirstOrDefault(iv1 => iv1.InputParameterId == iv.InputParameterId).ValueAsString != iv.ValueAsString))
                        .Count()
                }).Sum(tc => tc.killCount).ToString(),
            ldr.OriginalCode.Mutants.Count().ToString(),
            ldr.ASTDiff??"null"
        }.Concat(
            ldr.ParseSubTrees.OrderBy(pst => pst.Height).Select(pst => pst.SerializedTree).ToArray()
            ).ToArray()).ToArray();

Don't worry about the above code. The problem is that I want the matrix to be square. the columns which are hard coded are OK, but those that come from DB ldr.ParseSubTrees.OrderBy(pst => pst.Height).Select(pst => pst.SerializedTree).ToArray() make problem. Before reaching this statement (big statement) I have queried the DB for the max size. So I know the size and just want something like this:

ldr.ParseSubTrees.OrderBy(pst => pst.Height).Select(pst => pst.SerializedTree).ToArray(maxParseTreeDepth)

Although this is example of the thing I need and not the solution but I need to define the array size in a form like this.

And before you suggest solutions, I have to note that Array.Resize won't help. Because it needs a saved object as ref and in a select expression I can't do that.

0

2 Answers 2

3

A quick and dirty solution is to concat an array and than use Take():

Before the query:

var paddingArray = new string[maxParseTreeDepth];

Inside query:

ldr.ParseSubTrees
    .OrderBy(pst => pst.Height)
    .Select(pst => pst.SerializedTree)
    .Concat(paddingArray)
    .Take(maxParseTreeDepth)
    .ToArray();
Sign up to request clarification or add additional context in comments.

6 Comments

:D yes this is really dirty. This will create arrays three/four times! I hold this as the last way. any other Idea???
If you are afraid of multiple array creation, you may always create the array before and then use that instance inside Concat.
@ConductedClever "This will create arrays three/four times!" why do you say that? Only one array will get created in the loop - the one that is generated when you call ToArray.
I would not call this dirty at all. You need to pad arrays that are not big enough, which you do quite effectively.
Yes, I think you are right. Still not sure enough! But I can accept it for now.
|
0

If I understand your question correctly, then you want to get only a certain amount of rows. Maybe taking only x rows is what you're looking for:

ldr.ParseSubTrees
    .OrderBy(pst => pst.Height)
    .Select(pst => pst.SerializedTree)
    .Take(maxParseTreeDepth)
    .ToArray();

1 Comment

No. As this code shows > Console.WriteLine(arr.Take(4).Count()); > 3 take method just makes a ceiling. But I want to be exactly the length I set.

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.