-1

I want to convert this from SQL to Linq but I don't know the exact syntax:

SELECT TOP 1 
    R.GalleryId a, COUNT(*) b, G.Address ad
FROM 
    [PiArt].[dbo].[Rents] R, [PiArt].[dbo].[Galleries] G 
WHERE
    R.GalleryId = G.GalleryId
GROUP BY
    R.GalleryId, G.Address
ORDER BY 
    COUNT(*) DESC
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

2 Answers2

0

Try following

    class Program
    {
        static void Main(string[] args)
        {
            List<Gallery> galleries = new List<Gallery>();
            List<Rent> rents = new List<Rent>();


            var results = (from r in rents
                           join g in galleries on r.GallerId equals g.GallerId
                           select new { r = r, g = g })
                           .GroupBy(x => new { id = x.r.GallerId, address = x.r.Address })
                           .Select(x => new {
                               count = x.Count(),
                               id = x.Key.id,
                               address = x.Key.address
                           })
                           .OrderByDescending(x => x.count)
                           .FirstOrDefault();
        }
    }

    public class Gallery
    {
        public int GallerId { get; set; }
    }
    public class Rent
    {
        public int GallerId { get; set; }
        public string Address { get; set; }
    }
jdweng
  • 33,250
  • 2
  • 15
  • 20
0

you can try this.

from R in Rents
join G in Galleries on R.GalleryId equals G.GalleryId
group new {R,G} by new { R.GalleryId , G.Address}  into GRP 
orderby GRP.Count() descending
select new { 
                a= GRP.Key.GalleryId, 
                b = GRP.Count(), 
                ad = GRP.Key.Address  
            }
Serkan Arslan
  • 13,158
  • 4
  • 29
  • 44