将存储过程转换为 LINQ 查询

本文关键字:LINQ 查询 转换 存储过程 | 更新日期: 2023-09-27 18:35:14

    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 

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

not:AppCache 等同于 App

将存储过程转换为 LINQ 查询

这应该是存储过程的翻译。如果要返回其他内容,只需修改 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() }