1

I'm trying to create a multidimensional array and afterwords using this same multidimensional array to create charts, but I don't know how to make this.

The logic I'd like to achive is below.

//T070_RESULT is an entity .Linq

public int id { get; set; }
public string entidade { get; set; }
public Nullable<int> entidadeID { get; set; }
public string unidadeMedida { get; set; }        //not relevant for this question
public string valor { get; set; }
public Nullable<System.DateTime> date { get; set; }

//this list contains objects with the structure above
List<T070_RESULT> resultados = ListOfResults(idSample);

imagine this example (resultados list):

[id, entidade, entidadeID, unidadeMedida, valor, date] 
[1, EntityX, 10, 10, 100, 01-09-16]
[2, EntityX, 10, 10,  80, 02-09-16]
[3, EntityX, 10, 10, 120, 01-10-16]
[4, EntityZ, 10, 10, 100, 01-09-16]
[5, EntityZ, 10, 10, 110, 01-10-16]

now I'd like to create a multidimensional array grouped by date and having this results below:

new object would be generated from the input above:

EntityX_Value(summed values for that month of that year) would be one column with all values grouped by date

EntityZ_Value the same

[id, EntityX_Value(summed values for that month of that year), EntityZ_Value, date]
[1, 180, 100, 01-09-16] 
[2, 80,  110, 01-10-16]

(regarding dates, i don't need the day, i just need this grouped by month and year)

how can I do this, using linq or other methodology ?

7
  • 2
    There are a few problems with this question. 1) There is no multidiensional array anywhere. 2) 09-16 is not a representation of a DateTime. 3) what is EntityX_Value and EntityZ_Value? (your input has unidadeMedida and valor) 4) where does the id in the result come from? Commented Aug 31, 2016 at 8:41
  • Or, more basically, explain how you get from your example input to your example output, and the answer will be easy. Commented Aug 31, 2016 at 8:43
  • you want to get an output of string[]? Commented Aug 31, 2016 at 8:46
  • maybe the multidimensional array is not the best aproach at all, maybe other kind of object to achieve this purpose. Commented Aug 31, 2016 at 8:53
  • @Jamiec please review the question, I hope i've ansewered to your, regarding the id it doesent't matter how is generated it can be a sequence. Commented Aug 31, 2016 at 8:58

2 Answers 2

1

This is a bit tricky, based on the fact you commented that entities could by dynamic (ie, more than just EntityX and EntityZ).

The first thing to do is work out which entities you have, thats as easy as selecting them from the list and using Distinct. Then you need to group by month & year, and use the entity list to build up something like a dictionary (key=Entity name value=sum of valor).

This would work.

// get a distinct list of entities
var entities = list.Select(x => x.entidade).Distinct();

// group your original list, and use the above list to search for the right results
var result = list.GroupBy( x => new {Month = x.date.Value.Month, Year = x.date.Value.Year})
    .Select( (x,i) => new {
        id = (i+1),
        values = entities.ToDictionary(k => k, v => x.Where(y => y.entidade == v).Sum(y => y.valor)),
        date = new DateTime(x.Key.Year, x.Key.Month,1)
    });

Live example: http://rextester.com/RGV51846

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

4 Comments

Excuse my use of english variable names. Without knowing how to pluralise entidade and valor I did not want to confuse things.
no problem, my bad! just one question, i've tested but the Sum(y => y.valor) is not adding the other values from the same dates, regarding the rest I think is this what i was looking for
@PTLearner - I made one assumption, and changed one thing. In your example valor is a string. I changed it to an int. You can always do .Sum(y => int.Parse(y.valor)) (or double.Parse if more appropriate). Better would be to use a numerical type in the first place if you're doing arithmetic on it. Look at my live example if you're unsure.
yes I've done that, I'll look into it and set the ansewer as correct as soon I finnish this
0

Bit confusing, but taking from your expected output, you need grouping on date and do something like this.

var result = resultados.GroupBy(x=>x.date)
          .Select((x,i)=>
           {
               id = i+1,
               EntityX_Value = x.Where(w=>w.entidade == "EntityX").Sum(s=>s.valor),
               EntityZ_Value = x.Where(w=>w.entidade == "EntityZ").Sum(s=>s.valor), 
               date = x.Key 
           });

3 Comments

it's almost this, i've given that example but we have to assume that the number of entities increase or might be only one, how can i do this dinamiclly ?
Hmm, that's bit challenging but what you could do is grouping again entitiy and then try to pivot the results by grouping them on date again.
well that seems to work, but i'm a bit new at .Linq and i don't know how to make a pivot table using this aproach

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.