How do I convert this to using Linq to dataset

  • Thread starter Thread starter Tony
  • Start date Start date
T

Tony

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

//tony
 
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
 
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
 
"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
 
"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
 
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
 
"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
 
"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
 
"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
 
Back
Top