We help IT Professionals succeed at work.
Get Started
Private
Troubleshooting Question

LINQ:  ROW_NUMBER() OVER(PARTITION BY  ORDER BY

39 Views
Last Modified: 2020-08-28
Hi Experts!

Need your help with the converting the following SQL to LINQ:

SELECT make, 
       ROW_NUMBER() OVER(PARTITION BY make
       ORDER BY ISNULL(WarrantyDateTo, '12/31/2099') DESC,
                CASE
                    WHEN Condition = 'poor' AND [Year] = '2020' THEN 'z'
                    ELSE 'a'
                END) AS 'rn', 
       Id, 
       WarrantyDateTo, 
       Condition, 
       [Year]
FROM dbo.cars;

Here's the attempted LINQ:
namespace Provider.Extracts.Tests.Services
{

    public class car
    {
        public int Id { get; set; }
        public DateTime? WarrantyDateTo { get; set; }
        public string Make { get; set; }
        public string  Condition { get; set; }
        public string Year { get; set; }
    }

    [TestClass]
    public class SampleTests
    {
        [TestMethod]
        public void test2()
        {
            var cars = new List<car>() {
                   new car { Id=1 , WarrantyDateTo = null , Make = "Ford", Condition = "exellent", Year = "2010"  },
                new  car { Id=2 ,   WarrantyDateTo = new DateTime(2030,12,31), Make = "Ford", Condition = "poor"    , Year = "2020"  },
                new  car { Id=3 ,   WarrantyDateTo = new DateTime(2030,12,31), Make = "Ford", Condition = "unknown" , Year = "2017"  },
                new car { Id=1 , WarrantyDateTo = null , Make = "Toyota", Condition = "exellent", Year = "2010"  },
                new  car { Id=2 ,   WarrantyDateTo = new DateTime(2030,12,31), Make = "Toyota", Condition = "poor"    , Year = "2020"  },
                new  car { Id=3 ,   WarrantyDateTo = new DateTime(2030,12,31), Make = "Toyota", Condition = "unknown" , Year = "2017"  }
            };


            var results =
                cars.OrderBy(x => x.WarrantyDateTo ?? new DateTime(2099, 12, 31))
                                .OrderBy(x => x.Condition == "poor" && x.Year.ToUpper() == "2020" ? "z" : "a")
                                .GroupBy(x => x.Make)
                                .Select(g => new { g, count = g.Count() })
                                .SelectMany(t => t.g.Select(b => b)
                                .Zip(Enumerable.Range(1, t.count),
                                (j, i) => new 
                                {
                                    j.Make,
                                    rn = i,
                                    j.Id,
                                    WarrantyDateTo = j.WarrantyDateTo ?? new DateTime(2099, 12, 31),
                                    j.Condition,
                                    j.Year,
                                })).ToList();
        }
    }


}



Expected Results
Make   Rn   Id   WarrantyDateTo Condition  Year
Ford   1    3    12/31/2020     unknown    2017
Ford   2    2    12/31/2020     poor       2020
Ford   3    1    12/31/2099     exellent   2020

Toyota 1    3    12/31/2020     unknown    2017
Toyota 2    2    12/31/2020     poor       2020
Toyota 3    1    12/31/2099     exellent   2020



Actual Results
Make   Rn   Id   WarrantyDateTo Condition  Year
Ford   1    3    12/31/2020     unknown    2017
Ford   2    1    12/31/2099     exellent   2020
Ford   3    2    12/31/2020     poor       2020

Toyota 1    3    12/31/2020     unknown    2017
Toyota 2    1    12/31/2099     exellent   2020
Toyota 3    2    12/31/2020     poor       2020

TIA!
Comment
Watch Question
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant

An Experts Exchange subscription includes unlimited access to online courses.

Get Started
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE