0

I have two tables/entities, event and eventRegistrations. I have a gridview showing events, but I would like it to have a column that sums eventRegistrations.SomeNumber.

I can get a count of eventRegistrations using the navigation object.

How can I get the sum?

M Kenyon II
  • 4,136
  • 4
  • 46
  • 94
  • So, I created a method, `GetSum(object _eventReg)` pass it the Navigation from the grid view like so: <%# GetSum(Eval("EventRegistrations")) %> and the object is type `Generic.List(of EventRegistrations)`. I then redo the method 'GetSum(List _eventReg)` but that fails. _CS1502: The best overloaded method match for 'EventRegManager._Default.GetRegSum(System.Collections.Generic.List)' has some invalid arguments_ – M Kenyon II Apr 27 '12 at 16:11

2 Answers2

0

If you can count the event registrations with event.EventRegistrations.Count() you should be able to get the sum using the LINQ Sum extension method of IEnumerable<T>:

var sum = event.EventRegistrations.Sum(er => er.SomeNumber);
Slauma
  • 175,098
  • 59
  • 401
  • 420
  • So, that looks like I should put the code in the GridView.OnDataBind event? Or is there a way to put that in the markup? – M Kenyon II Apr 27 '12 at 14:51
  • @MKenyonII: I don't know, I'm not really familiar with WebForms anymore. I remember that there is an option to create custom columns and fill them `Eval`, perhaps this might work. How do you bind the `Count`? I'd expect that binding the `Sum` works the same way. – Slauma Apr 27 '12 at 15:59
0

Handled it like this:

public string GetRegSum(object _eventReg)
{
System.Collections.Generic.List<FormRouterData.family_fishing_reg> theseRegs = (System.Collections.Generic.List<FormRouterData.family_fishing_reg>)_eventReg;
var sum = theseRegs.Sum(er => int.Parse(er.num_group));
if (sum == 0) { return string.Empty; }
else { return sum.ToString(); }
}

M Kenyon II
  • 4,136
  • 4
  • 46
  • 94