On 5/15/2013 2:36 AM, Tony wrote:
> "Arne Vajhøj" wrote in message
> news:5192c12a$0$32108$1472...@news.sunsite.dk...
> > On 5/14/2013 6:54 PM, Arne Vajhøj wrote:
>>> On 5/14/2013 2:14 AM, Tony wrote:
>>>> "Arne Vajhøj" wrote in message
>>>> news:5191777b$0$32118$1472...@news.sunsite.dk...
>>>>> On 5/13/2013 6:22 PM, Arne Vajhøj wrote:
>>>>>> On 5/13/2013 12:27 PM, Tony wrote:
>>>>>>> foreach (dsFgodsdagTime.ttFgodsdagTimeRow row in
>>>>>>> dsFgodsdagTimeWait.ttFgodsdagTime.Rows)
>>>>>>> {
>>>>>>> if (row.antalg > 0)
>>>>>>> sumtid += row.tim * row.antalg;
>>>>>>> else
>>>>>>> sumtid += row.tim;
>>>>>>> }
>>>>>>
>>>>>> Try:
>>>>>>
>>>>>> dsFgodsdagTimeWait.ttFgodsdagTime.Rows.Aggregate(0, (sumtid, row) =>
>>>>>> sumtid += row.antalg > 0 ? row.antalg * row.tim : row.tim)
>>>>>
>>>>> Or shorter:
>>>>>
>>>>> dsFgodsdagTimeWait.ttFgodsdagTime.Rows.Sum((row) => row.antalg > 0 ?
>>>>> row.antalg * row.tim : row.tim)
>>>>
>>>> Doesn't compile
>>>> DataRowCollection Rows doesn't take either Sum or Aggregate
>>>
>>> What about:
>>>
>>> dsFgodsdagTimeWait.ttFgodsdagTime.AsEnumerable().Sum((row) => row.antalg
>>> > 0 ? row.antalg * row.tim : row.tim)
>>
>> If it does not work, then I would conclude that DataSet and
>> LINQ is not a good combo.
>
> It works fine now.
> Can you explain in text so I can understand the solution.
> For example how should I understand the symbol =>
lst.Sum((d) => d.N > 0 ? d.N * d.T : d.T)
is a short form for:
lst.Sum((Func<Data,int>)MySum)
...
public static int MySum(Data d)
{
return d.N > 0 ? d.N * d.T : d.T;
}
Arne