使用 linq 和 c# 的最大父 ID 的子节点数
Posted
技术标签:
【中文标题】使用 linq 和 c# 的最大父 ID 的子节点数【英文标题】:number of child with max parent id with linq and c# 【发布时间】:2021-11-20 17:25:26 【问题描述】:有什么方法可以获取具有最大父 ID 的孩子的数量并使用 linq 列出结果? 我试图通过Status带来值的总和,但我只能从孩子那里得到最后一个 到目前为止我做了什么:
using System;
using System.Collections.Generic;
using System.Linq;
public class Program
public static void Main()
var lstRfq = new List<RfqEvents>()
new RfqEvents(1,1,DateTime.Parse("2021-05-06 03:00:00+00"),1),
new RfqEvents(2,2,DateTime.Parse("2021-05-06 03:00:00+00"),1),
new RfqEvents(3,2,DateTime.Parse("2021-05-06 03:00:00+00"),1),
new RfqEvents(4,3,DateTime.Parse("2021-05-06 00:00:00+00"),2),
new RfqEvents(5,4,DateTime.Parse("2021-05-06 00:00:00+00"),2),
new RfqEvents(6,5,DateTime.Parse("2021-05-06 00:00:00+00"),2),
new RfqEvents(7,5,DateTime.Parse("2021-05-06 00:00:00+00"),2),
new RfqEvents(8,5,DateTime.Parse("2021-05-06 00:00:00+00"),3),
new RfqEvents(9,6,DateTime.Parse("2021-05-06 00:00:00+00"),3),
new RfqEvents(10,6,DateTime.Parse("2021-05-06 00:00:00+00"),3),
;
var subquery = from c in lstRfq
group c by c.RfqId into g
select new InternalStatusInformations
RfqId = g.Key,
RfqEventId = g.Max(a => a.Id),
StatusId = g.Select(p => p.Status).FirstOrDefault()
;
var sss = from d in lstRfq.Where(p=> subquery.Select(p=>p.RfqEventId).Contains(p.Id))
group d by d.Status into z
select new InternalStatusInformations
StatusId = z.Key,
Total = z.Count(),
Past = z.Where(p => p.DueDate.HasValue && p.DueDate.Value.Date < DateTime.Now.Date).Count(),
Future = z.Where(p => p.DueDate.HasValue && p.DueDate.Value.Date > DateTime.Now.Date).Count(),
Today = z.Where(p => p.DueDate.HasValue && p.DueDate.Value.Date == DateTime.Now.Date).Count(),
FiveDays = z.Where(p => (p.DueDate.HasValue && p.DueDate.Value.Date > DateTime.Now.Date) && p.DueDate.HasValue && p.DueDate.Value.Date < DateTime.Now.Date.AddDays(5)).Count(),
;
//expected: Status 1: 2 values
// Status 2: 3 values
// Status 3: 2 value
//output: Status 1: 2 values
// Status 2: 2 values
// Status 3: 2 values
sss.Dump();
public class InternalStatusInformations
public int RfqEventId get; set;
public int RfqId get; set;
public int StatusId get; set;
public int Future get; set;
public int Past get; set;
public int Today get; set;
public int FiveDays get; set;
public int Total get; set;
public DateTime? DueDate get; set;
public class RfqEvents
public RfqEvents(int id, int rfqId, DateTime? dueDate, int status)
Id = id;
RfqId = rfqId;
DueDate = dueDate;
Status = status;
public int Id get; set;
public DateTime? DueDate get; set;
public int RfqId get; set;
public int Status get; set;
https://dotnetfiddle.net/YoRsIG
但是结果有些不对劲,你们能帮帮我吗?
【问题讨论】:
恕我直言,您应该提供一个示例并解释您要做什么。 【参考方案1】:如果您只想计算每个状态中不同 RfqId
值的数量,应该这样做:
var pairs = lstRfq
.GroupBy(evt => evt.Status)
.Select(grouping =>
var status = grouping.Key;
var count = grouping
.Select(evt => evt.RfqId)
.Distinct()
.Count();
return (status, count);
);
foreach ((var status, var count) in pairs)
Console.WriteLine($"Status status: count values");
输出是:
Status 1: 2 values
Status 2: 3 values
Status 3: 2 values
【讨论】:
以上是关于使用 linq 和 c# 的最大父 ID 的子节点数的主要内容,如果未能解决你的问题,请参考以下文章