Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How do I convert this to using Linq to dataset

23 views
Skip to first unread message

Tony

unread,
May 13, 2013, 12:27:06 PM5/13/13
to
foreach (dsFgodsdagTime.ttFgodsdagTimeRow row in
dsFgodsdagTimeWait.ttFgodsdagTime.Rows)
{
if (row.antalg > 0)
sumtid += row.tim * row.antalg;
else
sumtid += row.tim;
}

//tony

Arne Vajhøj

unread,
May 13, 2013, 6:22:15 PM5/13/13
to
Try:

dsFgodsdagTimeWait.ttFgodsdagTime.Rows.Aggregate(0, (sumtid, row) =>
sumtid += row.antalg > 0 ? row.antalg * row.tim : row.tim)

Arne



Arne Vajhøj

unread,
May 13, 2013, 7:30:04 PM5/13/13
to
Or shorter:

dsFgodsdagTimeWait.ttFgodsdagTime.Rows.Sum((row) => row.antalg > 0 ?

Tony

unread,
May 14, 2013, 2:14:06 AM5/14/13
to


"Arne Vajh�j" wrote in message
news:5191777b$0$32118$1472...@news.sunsite.dk...
Doesn't compile
DataRowCollection Rows doesn't take either Sum or Aggregate

//Tony

Arne Vajhøj

unread,
May 14, 2013, 6:54:14 PM5/14/13
to
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

Arne Vajhøj

unread,
May 14, 2013, 6:56:42 PM5/14/13
to
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.

Arne


Tony

unread,
May 15, 2013, 2:36:32 AM5/15/13
to


"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.

Arne

It works fine now.
Can you explain in text so I can understand the solution.
For example how should I understand the symbol =>

//Tony

mick

unread,
May 15, 2013, 5:19:50 AM5/15/13
to
=> is the symbol for lambda and what follows it is the lambda expression. Your
collection
is iterated through and each individual item is passed to the lambda to get its
result,
either row.antalg * row.tim or row.tim.

mick

Arne Vajhøj

unread,
May 15, 2013, 10:08:44 PM5/15/13
to
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

0 new messages