在下面的这种情况下,我一直在用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
答案 0 :(得分:-1)
这就是您要寻找的东西:
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; }
}