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!

Ruixin
  • 45
  • 6
  • 1
    Possible duplicate of [c# linq return a multidimensional array from linq](http://stackoverflow.com/questions/18896150/c-sharp-linq-return-a-multidimensional-array-from-linq) – layonez May 08 '16 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? – Ruixin May 08 '16 at 19:00
  • Check my answer, made it using code from @Damith answer from post i mention – layonez May 08 '16 at 19:07

1 Answers1

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;
}
layonez
  • 1,746
  • 1
  • 16
  • 20
  • Thank you, great help you are! – Ruixin May 08 '16 at 21:45
  • 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 ;) – layonez May 08 '16 at 22:30
  • Thank you, this is the first time I ask question on this website. – Ruixin May 08 '16 at 23:02