开发者

How to Convert this Sql query to LINQ?

开发者 https://www.devze.com 2023-03-02 11:30 出处:网络
I want to convert the below query to LINQ, SELECT AVG(currentvalue) AS ChartAvg, CONVERT(varchar(10), ReceivedDate, 103) AS RecDate

I want to convert the below query to LINQ,

  SELECT AVG(currentvalue) AS ChartAvg, 
         CONVERT(varchar(10), ReceivedDate, 103) AS RecDate
    FROM DatapointValues
   WHERE (CONVERT(varchar(10), ReceivedDate, 103) = '21/04/2011')
GROUP BY CONVERT(varchar(10), ReceivedDate, 103)

I use the below LINQ b开发者_开发百科ut not working,

from datapointvalues in db.DatapointValues
where
Convert.ToString(datapointvalues.ReceivedDate) == "21/04/2011"
group datapointvalues by new {
Column1 = Convert.ToString(datapointvalues.ReceivedDate)
} into g
select new { 
  ChartAvg = (System.Decimal?)g.Average(p => p.currentvalue),    
  g.Key.Column1    
}


Check out this article: http://msdn.microsoft.com/en-us/library/bb882657.aspx

var date = DateTime.Parse("21/04/2011");

var result = from datapointvalues in db.DatapointValues
             where datapointvalues.ReceivedDate.Date.equals(date)
             group datapointvalues by datapointvalues.ReceivedDate.Date
             into g
             select new
             { 
                 ChartAvg = g.Average(p => p.currentvalue),
                 RecDate = g.Key
             };
0

精彩评论

暂无评论...
验证码 换一张
取 消