Linq to SQL使用Group by

时间:2015-10-04 19:54:35

标签: entity-framework linq-to-sql linq-to-entities

我不确定如何使用linq to SQL

表达以下SQL
SELECT p.*, T1.RecordCount
FROM Person p
INNER JOIN 
(
    SELECT PersonId, count(1) as RecordCount FROM [PersonView]
    WHERE LastName like 'LIS%'
    GROUP BY PersonId 
) AS T1 ON T1.PersonId = p.PersonId

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

var innerQuery=from pv in context.PersonView
               where pv.LasName.StartWith("LIS")
               group pv by pv.PersonId into g
               select new{ PersonId=g.Key,RecordCount = g.Count()};

var query= from p in context.Person
           join t1 in innerQuery on p.PersonId equals t1.PersonId
           select new{p, t1.RecordCount};