[英]Compare values between 3 lists and add missing values from first list into the remaining 2 lists - C#
I need to prepare a chart wherein I'm required to show 3 lines. 我需要准备一张图表,其中需要显示3条线。
One for showing new issues for a week, second for closed issues for a week and third for total accumulative open issues from first week to last week. 一个用于显示一周的新问题,第二用于显示一周的未解决问题,第三用于显示从第一周到上周的累计未解决问题。
For this reason, I have prepared a query and was able to create 2 separate lists successfully - one list maintains the weekly count of new issues and second list maintains the weekly count of closed issues. 因此,我准备了一个查询并能够成功创建2个单独的列表-一个列表维护新问题的每周计数,第二个列表维护已解决问题的每周计数。
Here is the sample data for first list (one which maintains new issues) : 这是第一个列表的示例数据(其中包含新问题):
[0]: { Week = {6/14/2015 12:00:00 AM}, Count = 1 }
[1]: { Week = {3/5/2017 12:00:00 AM}, Count = 1 }
[2]: { Week = {5/21/2017 12:00:00 AM}, Count = 4 }
[3]: { Week = {6/4/2017 12:00:00 AM}, Count = 7 }
[4]: { Week = {6/11/2017 12:00:00 AM}, Count = 4 }
[5]: { Week = {6/25/2017 12:00:00 AM}, Count = 7 }
[6]: { Week = {7/9/2017 12:00:00 AM}, Count = 3 }
From the above data, I get the total count of open issues for a particular week. 从以上数据中,我可以得出特定一周未解决问题的总数。
Note: For both these lists the Week values contain date which falls on Sunday. 注意:对于这两个列表,“星期”值都包含星期天的日期。 As I need the week to start on Monday while displaying data in the chart. 因为我需要一周从星期一开始,同时在图表中显示数据。
Similarly for sample data for second list (one which maintains closed issues) : 同样,第二个列表的样本数据(其中一个保留了已解决的问题):
[0]: { Week = {12/13/2015 12:00:00 AM}, Count = 1 }
[1]: { Week = {7/9/2017 12:00:00 AM}, Count = 3 }
[2]: { Week = {6/18/2017 12:00:00 AM}, Count = 2 }
[3]: { Week = {7/23/2017 12:00:00 AM}, Count = 8 }
[4]: { Week = {10/1/2017 12:00:00 AM}, Count = 6 }
[5]: { Week = {8/6/2017 12:00:00 AM}, Count = 3 }
[6]: { Week = {9/17/2017 12:00:00 AM}, Count = 1 }
From the above data, I get total count of closed issues for a particular week. 从以上数据中,我可以得出特定一周内已完成问题的总数。
Here's the code for these lists : 这是这些列表的代码:
var openIssuesList = getDetails.Where(x => x.ChangedTo == "Open").Select(x => new { Week = x.Date.AddDays(x.Date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)x.Date.DayOfWeek).Date, Detail = x }).GroupBy(x => x.Week).Select(x => new { Week = x.Key, Count = x.Count() }).ToList();
var closedIssuesList = getDetails.Where(x => x.ChangedTo == "Closed").Select(x => new { Week = x.Date.AddDays(x.Date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)x.Date.DayOfWeek).Date, Detail = x }).GroupBy(x => x.Week).Select(x => new { Week = x.Key, Count = x.Count() }).ToList();
As I mentioned earlier, I need to prepare a weekly line chart using the data obtained from these lists. 如前所述,我需要使用从这些列表中获得的数据来准备每周折线图。 Now the final piece that remains is to fill in the missing week values into both of these lists using a third list which I have prepared. 现在剩下的最后一件工作是使用我准备的第三个列表将缺失的周值填写到这两个列表中。
I have prepared a list say datesList which contains all the dates (which fall on Sunday) between a specified date range. 我准备了一个列表,说“ datesList” ,其中包含指定日期范围之间的所有日期(属于星期天)。
Here's the sample data for this list : 这是此列表的示例数据:
[0]: {6/14/2015 12:00:00 AM}
[1]: {6/21/2015 12:00:00 AM}
[2]: {6/28/2015 12:00:00 AM}
[3]: {7/5/2015 12:00:00 AM}
[4]: {7/12/2015 12:00:00 AM}
[5]: {7/19/2015 12:00:00 AM}
[6]: {7/26/2015 12:00:00 AM}
//And so on ....
Explanation : 说明:
So from the above provided sample data for 3 lists here's how the newly updated openIssuesList list should like : (showing only few records) 因此,从上面提供的3个列表的示例数据中,这是新近更新的openIssuesList列表的样子:(仅显示少量记录)
[0]: { Week = {6/14/2015 12:00:00 AM}, Count = 1 } // Exisiting record hence no change for this record
[1]: { Week = {6/21/2015 12:00:00 AM}, Count = 0 } // As this date was not existing initially it's been added with Count value set to 0
[2]: { Week = {6/28/2015 12:00:00 AM}, Count = 0 } // As this date was not existing initially it's been added with Count value set to 0
//and so on....
The same logic applies for closedIssuesList. 相同的逻辑适用于closedIssuesList。
So datesList is kind of 'Master List' and we need to compare the other lists with this list. 因此, datesList是一种“主列表”,我们需要将其他列表与此列表进行比较。 If a week value present in datesList is missing from other lists then add this week value to other lists with the Count value being set to 0. 如果其他列表中缺少datesList中存在的星期值,则将该星期值添加到其他列表中,并将“ 计数”值设置为0。
EDIT : 编辑 :
Initially in the lists openIssuesList & closedIssuesList I was not selecting IDs of these issues. 最初,在清单openIssuesList和closedIssuesList中,我没有选择这些问题的ID。 Only the Week and Count of thee issues were being retrieved. 仅检索了“周”和“问题计数”问题。
So to select these IDs I updated the logic for these both lists as follows : 因此,为了选择这些ID,我更新了这两个列表的逻辑,如下所示:
var openIssuesList = getDetails.Where(x => x.ChangedTo == "Open").Select(x => new { Week = x.Date.AddDays(x.Date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)x.Date.DayOfWeek).Date, Detail = x }).GroupBy(x => x.Week).Select(x => new { Week = x.Key, Count = x.Count(), Issues = x.Select(y => y.Detail.IssueID).ToArray() }).ToList();
var closedIssuesList = getDetails.Where(x => x.ChangedTo == "Closed").Select(x => new { Week = x.Date.AddDays(x.Date.DayOfWeek == DayOfWeek.Sunday ? 0 : 7 - (int)x.Date.DayOfWeek).Date, Detail = x }).GroupBy(x => x.Week).Select(x => new { Week = x.Key, Count = x.Count(),Issues = x.Select(y => y.Detail.IssueID).ToArray() }).ToList();
Hence I just added this : 因此,我只是添加了这个:
Issues = x.Select(y => y.Detail.IssueID).ToArray()
into my final Select clause and it'll help fetch the IssueID's as well (while initially we just fetched the count of these IssuesID's) 放入我的最终Select子句中,它也将有助于获取IssueID的标识(最初,我们只是获取了这些IssuesID的计数)
So far so good. 到现在为止还挺好。
But now I want to include these IssuesID's into the @stybl provided solution as well. 但是现在我也想将这些IssuesID包含在@stybl提供的解决方案中。
So how should I modify my logic to accommodate IssueID's into @stybl 's solution. 因此,我应该如何修改逻辑以将IssueID纳入@stybl的解决方案中。
Trying this would obviously give an error : 尝试这样做显然会导致错误:
openIssuesList.Concat(datesList.Where(date => !openIssuesList.Any(pair => pair.Week == date)).Select(date => new { Week = date, Count = 0, Issues = x.Select(y => y.Detail.IssueID).ToArray() });
How do I select the appropriate IssueID's for this part? 如何为此部分选择适当的IssueID?
This should work for you: 这应该为您工作:
openIssuesList.Concat(datesList.Where(date => !openIssuesList.Any(pair => pair.Week == date))
.Select(date => new { Week = date, Count = 0 });
What we do here is we get the inverse of the intersect of the two lists based on date. 我们在这里所做的是基于日期获得两个列表的相交的倒数。 From that we select the anonymous object with the data, and finally append the resulting enumeration to openIssuesList
. 从中,我们选择带有数据的匿名对象,最后将结果枚举追加到openIssuesList
。
Likewise for closedIssuesList
: 同样对于closedIssuesList
:
closedIssuesList.Concat(datesList.Where(date => !closedIssuesList.Any(pair => pair.Week == date))
.Select(date => new { Week = date, Count = 0 });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.