1

Unfortunately I m new to Lambda/ Linq and I need to get data from a list. Below is my existing code:

        Highcharts chart = new Highcharts("chart")
         .InitChart(new Chart { DefaultSeriesType = ChartTypes.Bubble, ZoomType = ZoomTypes.Xy, Width = 600, Height = 400 })
         .SetTitle(new Title { Text = "Highcharts Bubbles" })
         .SetSeries(new[]
                {
                    new Series
                        {
                            Name="Buy",
                            Data = new Data(new object[]
                                {
                                    new object[] {97,36,79 },
                                    new object[] { 94,74,60 },
                                    new object[] { 68,76,58 }

                                })
                        }                           
                });

Lines of code which need to be replaced are:

new object[] {97,36,79 },
new object[] { 94,74,60 },
new object[] { 68,76,58 }

In above code inside {} instead of hardcoded values for example 97, 36, 79 in first line I need to pass values from transactions (transaction volume, price and termdate).

(it is just like using foreach loop but I have to do it using Lambda/ Linq.) I can query all transactions in list transaction.

My poco class is:

public class Transaction : SearchCondition, Automappable
    {
        [ScriptIgnore]
        public virtual int Id { get; set; }
        public virtual decimal Volume { get; set; }
        public virtual decimal Price { get; set; }
        public virtual DateTime TermDate { get; set; }
}

Can you please help me modifying code.

I highly appreciate your time, guidance and help.

EDIT I have tried the ways mentioned in below solutions, it is almost what is required but still a small issue.

Below codes are able to supply data but they cover data with an additional array. To clear it more please see below image of incorrect structureenter image description here: It do not show an error but because of that I am not able to use data.

Below is a snap short of correct data structure. enter image description here

Can you please help a little more.

1
  • Can you show us the code where you are passing your list into your Select statement? I think you must be somehow using a list with only one entry. Commented Nov 18, 2013 at 2:58

4 Answers 4

2

How about generating the array like this, where transactionList is your list of transactions:

Data = new Data(transactionList
               .Select(i => new object[] 
                       {i.Volume, i.Price, i.TermDate} as object)
               .ToArray();

Here you create multiple arrays of objects, one for each of your transaction records. Each of these arrays is treated as a single object in the final array.

Select works by mapping each element of an input IEnumerable (in your case, transactionList), to a function of the element. In your case, your function (the lambda expression inside the Select statement) takes each element, and extract fields into an object array.

The whole operation returns an IEnumerable of object - but your Data constructor takes an object array. So we have to call ToArray() to convert to an object[].

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

1 Comment

thanks. I just tried your suggestion. It is almost working but a small piece is still need to look at. Can you please edit and advice any change further ?
2

Have you tried using one of the above solutions but not selecting an Array of Arrays?

Using a List<Transaction> called transactions:

    Data = new Data(transactions.Select(t => t).ToArray());

This is assuming that the transactions IEnumerable is a collection of Transaction objects, in which case you just want to select every object in the array and pass to the Data classes constructor.

Alternatively, you could also do:

    Data = new Data(transactions);

2 Comments

my Tranansation Poco class has some other properties as well but I m just using 3. Can you please suggest me how it filter just requried columns ?
Toubi, using anonymous classes: var Data = transactions.Select(o => new {o.Id, o.Price, o.TermDate}).ToArray();
1

I you have a List<Transaction> object named tList, then your data line would look like this:

Data = new Data(tList
         // for each item in the list
         .Select(
        // create an array of the values we care about
          item => new object[] { item.Volume, item.Price, item.TermDate })
        // put each of those results into an array
        .ToArray());

2 Comments

thanks. I have tried your suggested code, it is almost there but still some issue. Can you please see my edit and advice further. Thanks
@Toubi - I don't believe you used the code exactly as I have it here and got what was showing in the debugger.
1

If you have an IEnumerable of objects you can use the Linq Select method to transform it into an IEnumerable of a different type.

The Select statement takes as a parameter a lambda expression which takes one item from the source IEnumerable and returns a (usually) different type. The results of the lambda expression become the items in a new IEnumerable.

In your case, let's assume that you have a List<Transaction> called transactions that contains all of the Transaction objects you want to display in the graph. Assuming that the items in the list are already sorted into the correct order, you can use the following code to create your Data instance:

Data = new Data(
    transactions.Select(t => 
        (object)new object[] { t.Volume, t.Price, t.TermDate}
    )
    .ToArray()
)

In this code the Select method is used to convert your List<Transaction> to IEnumerable<object>, which the ToArray method then converts to object[].

If you need to you can do conversions and calculations on the data inside the lambda expression:

Data = new Data(
    transactions.Select(t => 
        (object)new object[] 
        {
            (int)t.Volume,
            (int)t.Price,
            t.TermDate.DayOfYear
        }
    )
    .ToArray()
)

(Edited to return object[] instead of object[][])

1 Comment

can you please see my edit and guide. I tried your suggested way but still there is a little issue. Thanks

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.