vini
vini

Reputation: 4732

Converting a stored procedure to a LINQ query

    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;
    }

Stored Procedure:

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 

Can't figure out how to get the item count from the list.

Not: AppCache is equivalent to App

Upvotes: 2

Views: 1334

Answers (1)

Esteban Elverdin
Esteban Elverdin

Reputation: 3582

This should be the translation of your stored procedure. If you want to return something else, just modify the select method.

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() }

Upvotes: 2

Related Questions