将存储过程转换为LINQ查询

时间:2013-10-14 18:26:42

标签: c# sql linq stored-procedures

    public static IEnumerable<AppCache> GetTopRatedApps(string language,bool isinitialized)
    {
        List<AppCache> objApps = new List<AppCache>();

        objApps = GetAllApps(isinitialized,language).ToList();

        List<RatingCache> objRatings = new List<RatingCache>();

        objRatings = GetAllRatings();

           var query =
  from Apps in objApps
  join ratings in objRatings
      on Apps.AppId equals ratings.AppId where ratings.RatingGiven == 1 
  select new AppCache();

        return query;
    }

存储过程:

select o.AppId, count(*) as ItemCount 
from App o 
inner join Rating od 
    on o.AppId = od.AppId 
where od.RatingGiven = 1
group by o.AppId 

无法弄清楚如何从列表中获取项目数。

不是:AppCache等同于App

1 个答案:

答案 0 :(得分:2)

这应该是存储过程的翻译。如果你想返回别的东西,只需修改select方法。

var query = from Apps in objApps
            join ratings in objRatings
              on Apps.AppId equals ratings.AppId
            where ratings.RatingGiven == 1 
            group Apps by Apps.AppId into g
            select new { AppId = g.AppId, ItemCount = g.Count() }