tags:

views:

135

answers:

2

I'm trying to figure out the correct syntax to make it work. Here's an example. Could you correct it/translate it into linq syntax?

      From p In products() 
      group p by p.Category into g 
      select new { Category = g.Key, 
                   TotalUnitsInStock = if(g.key="b", g.Avg(p => p.UnitsInStock),
                                          g.Sum(p => p.UnitsInStock))

Products would be a datatable in this example. Thanks.

+2  A: 

Try the following

  var query = from p In products() 
              group p by p.Category into g 
              select new { Category = g.Key, 
                  TotalUnitsInStock = (g.key=="b") ? g.Avg(p => p.UnitsInStock) :
                                      g.Sum(p => p.UnitsInStock));
JaredPar
+3  A: 
from p in products()
group p by p.Category into g
select new
{
    Category = g.Key,
    TotalUnitsInStock = g.Key == "b"
        ? g.Average(p => p.UnitsInStock)
        : g.Sum(p => p.UnitsInStock)
}
Bryan Watts