将SQL内部选择转换为Linq转换为SQL

时间:2014-07-30 11:59:39

标签: sql-server-2008 linq-to-sql

有人可以帮我转换为Linq to SQL

SELECT
   a.dtCreated, COUNT(a.dtCreated) 
FROM 
    (SELECT     
        CONVERT(VARCHAR(12), dtCreated, 106) AS dtCreated
     FROM tblNotes) a
GROUP BY 
    a.dtCreated
ORDER BY 
    a.dtCreated 

由于

2 个答案:

答案 0 :(得分:0)

您尚未定义语言:

C#

var query = tblNotes
    .GroupBy(x=>x.dtCreated)
    .Select
    (
        x => new 
        {
            dtCreated = x.Key.ToString("dd mmm yyyy"), 
            Count = x.Count()
        }
    )
    .OrderBy(x=>x.dtCreated);

VB

Dim query = tblNotes.GroupBy(Function(x) x.dtCreated) _
    .Select(Function(x) New With _
        { _
            .dtCreated = x.Key.ToString("dd mmm yyyy"), _
            .Count = x.Count() _
        }) _
    .OrderBy(Function(x) x.dtCreated)

答案 1 :(得分:0)

假设dtCreateddatetime且有一个您想要消除的时间组件,并进一步假设您仍希望将dtCreated视为排序日期:

var q1 = tblNotes.GroupBy(n => n.dtCreated.Date)  // Group by the Date property of this DateTime
                 .OrderBy(g => g.Key)  // Order by the key, which is the Date portion of dtCreated
                 .Select(g => new 
                     { 
                          dtCreated = g.Key.ToString("dd mmm yyyy"), 
                          Count = g.Count()
                     });