0

I have a method that returns a simple int[2]. Both elements of the array need to each be assigned to an int r and int c local variables. I want to achieve this all within a single Linq query. Any way to do this?

This is pseudo code of what I want to achieve, but obviously it doesn't work. I don't know Linq very well and I'm trying to get better at it. Method(r,c) is the method that returns an int[2]. I want to pull each element out and assign int[0] = r and int[1] = c.

void Foo(int r, int c)
{
    Method(r,c).Select(([0],[1]) => { r = [0]; c = [1]; });
}
int[] Method(int r, int c)
{
    ///stuff///
}
0

1 Answer 1

1

logic:

  1. Method return int array with r and c
  2. create static class to make a Select method
  3. Select method input the int array and call Func<int[],T> and retrun T (T is Generic)

Online Test Demo Link

public class Program
{
    public static void Main()
    {
        var result = Method(1, 2).Select( (r,c) => new { r,c });
        Console.WriteLine(result);
    }
    static int[] Method(int r,int c) => new[] {r,c};
}

public static class LinqExtension
{
    public static T Select<T>(this int[] ints, Func<int,int, T> func) => func(ints[0],ints[1]);
}

enter image description here

or you can use Method with params int[]


public class Program
{
    public static void Main()
    {
        var value = Method(1, 2).Select((int[] arr) => new{r = arr[0],c = arr[1]});
        Console.WriteLine(value); //result : { r = 1, c = 2 }
    }

    public static int[] Method(params int[] ints)
    {
        return ints;
    }
}

public static class LinqExtension
{
    public static T Select<T>(this int[] ints,Func<int[],T> func){
        return func(ints);
    }   
}

new question :

How would I use in out keywords in this context? Assuming I'm using parameters passed by value from Foo().

you can use out keyword :

public class Program
{
    public static void Main()
    {
        Method(1, 2).Select( out int r ,out int c);
        Console.WriteLine(r);
        Console.WriteLine(c);
    }
    static int[] Method(int r,int c) => new[] {r,c};
}

public static class LinqExtension
{
    public static void Select(this int[] ints, out int r, out int c) 
    {
        r = ints[0];
        c = ints[1];
    }
}
Sign up to request clarification or add additional context in comments.

5 Comments

That helped out great! Thank you. Quick question though, in my pseudo-code example the method Foo(int r, int c) has two parameters, I'm using these in the Linq query and giving r = arr[0] and c = arr[1], why do I have to assign the output of this query into var value = ...? Is there a way to do this were I just query and assign r and c variables and move on to use my newly retrieved values of r and c in Foo(int r, int c)?
How would I use in out keywords in this context? Assuming I'm using parameters passed by value from Foo().
i update the answer for your new question @MatthewJett
Awesome, thank you so much @IT WeiHan. You really went above and beyond. Wish I could do more to to help your credibility than just upvote your answer.
It's my pleasure : )

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.