How do I convert this to using Linq to dataset

T

Tony

foreach (dsFgodsdagTime.ttFgodsdagTimeRow row in
dsFgodsdagTimeWait.ttFgodsdagTime.Rows)
{
if (row.antalg > 0)
sumtid += row.tim * row.antalg;
else
sumtid += row.tim;
}

//tony
 
A

Arne Vajhøj

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)

Arne
 
A

Arne Vajhøj

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)

Arne
 
T

Tony

"Arne Vajhøj" wrote in message

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)

Arne

Doesn't compile
DataRowCollection Rows doesn't take either Sum or Aggregate

//Tony
 
A

Arne Vajhøj

"Arne Vajhøj" wrote in message


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)

Arne
 
A

Arne Vajhøj

What about:

dsFgodsdagTimeWait.ttFgodsdagTime.AsEnumerable().Sum((row) => row.antalg

If it does not work, then I would conclude that DataSet and
LINQ is not a good combo.

Arne
 
T

Tony

"Arne Vajhøj" wrote in message

What about:

dsFgodsdagTimeWait.ttFgodsdagTime.AsEnumerable().Sum((row) => row.antalg

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
 
M

mick

"Arne Vajhøj" wrote in message



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 =>

=> 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
 
A

Arne Vajhøj

"Arne Vajhøj" wrote in message


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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top