0

I use entity framework, one entity called "interest rate", the Entity Set Name is “InterestRateSet”.

There are two properties, Time and Rate, both are double. The data for example, firstcolumn 0, 0.5, 1, secondcolumn 0, 0.01, 0.015

How can I get a 3row2column array using LINQ from InterestRateSet? Would someone show me the code example please? Thanks in advance!

3
  • 1
    Possible duplicate of c# linq return a multidimensional array from linq Commented May 8, 2016 at 18:57
  • Hello Layonez, thank you very much for you reply. I am really a beginner, would you show me an code example please? Commented May 8, 2016 at 19:00
  • Check my answer, made it using code from @Damith answer from post i mention Commented May 8, 2016 at 19:07

1 Answer 1

2
public string[,] GetInterestRates()
{
    var array  =(from ir in ctx.InterestRateSet                     
                 select new List<string>{ ir.Time.ToString() , ir.Rate.ToString()}).ToArray();

    return CreateRectangularArray(array);  
}


static T[,] CreateRectangularArray<T>(IList<T>[] arrays)
{
    // TODO: Validation and special-casing for arrays.Count == 0
    int minorLength = arrays[0].Count();
    T[,] ret = new T[arrays.Length, minorLength];
    for (int i = 0; i < arrays.Length; i++)
    {
        var array = arrays[i];
        if (array.Count != minorLength)
        {
            throw new ArgumentException
                ("All arrays must be the same length");
        }
        for (int j = 0; j < minorLength; j++)
        {
            ret[i, j] = array[j];
        }
    }
    return ret;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, great help you are!
Typically, when something works, you should click on the little check box next to that answer to "mark it as the answer" - this will help your account over time, as it shows that your questions get answered when you ask them later ;)
Thank you, this is the first time I ask question on this website.

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.