LINQ - AVG in LINQ - Asked By Shan on 03-May-11 08:12 AM

Hi,
 I wanted to convert this below query as LINQ
 

SELECT AVG(2) AS ChartAvg, CONVERT(datetime, ReceivedDate, 103) AS RecDate

FROM DatapointValues

GROUP BY ReceivedDate

ORDER BY ReceivedDate

 I tried this but the date value I am unable to fetch

var query = from d in context.DatapointValues

group d by d.ReceivedDate into g

select new

{

ChartAvg = g.Average(d => d.currentvalue),


};

dipa ahuja replied to Shan on 03-May-11 08:16 AM
Ravi S replied to Shan on 03-May-11 08:20 AM
HI

refer  your query by using this example

Select C.sName as CustomerName, S.sName as Salesperson,
Sum(O.nQty) as Qty, Sum(O.mAmount) as Amount
From tOrders O
Inner Join tCustomer C On C.CustomerID = O.CustomerID
Inner Join tSalesperson S On S.SalespersonID = O.SalespersonID
Group By C.sName, S.sName

and in linq is

var query = from order in db.Orders
group order by new { Customer = order.Customer,
SalesPerson = order.SalesPerson }
into grp
select new
{
Customer = grp.Key.Customer.Name,
SalesPerson = grp.Key.SalesPerson.Name,
Quantity = grp.Sum(o => o.Quantity),
Amount = grp.Sum(o => o.Amount)
};

foreach (var item in query)
{
Console.WriteLine("{0}, {1}, {2}, {3}", item.SalesPerson,
item.Customer, item.Amount, item.Quantity);
}


hope it helps you...
TSN ... replied to Shan on 03-May-11 08:22 AM
Hi..

var query = from d in context.DatapointValues

group d by d.ReceivedDate into g

select new

{

ChartAvg = g.Average(d => d.currentvalue),

 RecDat=String.Format("{0:d}", g.ReceivedDate.ToString);      // "3/9/2008"

};



use this and tell me...