Linq group by 和 select 子组中出现次数最多的项目

Linq group by and select most occuring item in sub group

我一直在努力为下面的这种情况在 c# 中编写一个 linq 查询,我得到了一个结构列表

PostCode, Premise, Connectivity, Availability
XYZ 123,    1,       HIGH,         TRUE
XYZ 123,    2,       LOW,          TRUE
XYZ 123,    3,       LOW,          FALSE
ABC 234,    1,       HIGH,         FALSE
ABC 123,    2,       HIGH,         FALSE

我正在尝试使用 ROW_NUMBER 和分区生成我在 SQL 中完成的以下内容,但这对我来说在 C# 中实现是一个挑战。非常感谢任何帮助。

PostCode,  Connectivity, Availability
XYZ 123,         LOW,          TRUE
ABC 234,         HIGH,         FALSE

这就是您要查找的内容:

class Program
{
    static void Main(string[] args)
    {
        List<PostCode> postCodes = new List<PostCode>() {
            new PostCode() {postcode = "XYZ 123", premise = 1, connectivity = "HIGH", availability = "TRUE"},
                new PostCode() {postcode = "XYZ 123", premise = 2, connectivity = "LOW", availability = "TRUE"},
                new PostCode() {postcode = "XYZ 123", premise = 3, connectivity = "LOW", availability = "FALSE"},
                new PostCode() {postcode = "ABC 234", premise = 1, connectivity = "HIGH", availability = "FALSE"},
                new PostCode() {postcode = "ABC 123", premise = 2, connectivity = "HIGH", availability = "FALSE"}
            };

            var results = postCodes.GroupBy(x => x.postcode)
                .Select(x => x.GroupBy(y => y.premise).Select(z => new { item = z, count = z.Count() }).ToList())
                .ToList();
        }
    }
    public class PostCode
    {
        public string postcode {get; set;}
        public int premise { get; set; }
        public string connectivity { get; set; }
        public string availability { get; set; }

    }