Reputation: 238
I have this .NET 8 console application:-
var workOrderList = context.Web.Lists.GetByTitle("Work Orders", p => p.Title,
p => p.Fields.QueryProperties(p => p.InternalName,
p => p.FieldTypeKind,
p => p.TypeAsString,
p => p.Title));
// Build a query that only returns these fields for the Work Orders
string viewXml = @"<View Scope='RecursiveAll'>
<ViewFields>
<FieldRef Name='Title' />
<FieldRef Name='Created' />
<FieldRef Name='FileLeafRef' />
<FieldRef Name='WorkOrderStatus' />
<FieldRef Name='WorkOrderType' />
<FieldRef Name='TechnicianName' />
<FieldRef Name='FileLeafRef' />
<FieldRef Name='AssetID' />
</ViewFields>
<Query>
<Eq>
<FieldRef Name='FSObjType' />
<Value Type='Integer'>0</Value>
</Eq>
</Query>
</View>";
// Load all the needed data using paged requests
bool paging = true;
string nextPage = null;
while (paging)
{
var output = await workOrderList.LoadListDataAsStreamAsync(new RenderListDataOptions()
{
ViewXml = viewXml,
RenderOptions = RenderListDataOptionsFlags.ListData,
Paging = nextPage ?? null,
}).ConfigureAwait(false);
if (output.ContainsKey("NextHref"))
{
nextPage = output["NextHref"].ToString().Substring(1);
}
else
{
paging = false;
}
}
var groupedWorkOrders = workOrderList.Items.AsRequested().Where(a => (a["WorkOrderType"] != null && a["WorkOrderType"].ToString() == "Cleaning") || (a["WorkOrderType"] != null && a["WorkOrderType"].ToString() == "Maintenance"))
.Select(wo =>
{
// Attempt to safely get each property, providing defaults for missing or null values
var technicianName = wo["TechnicianName"] != null ? TechNameID.FirstOrDefault(a => a.Key == (wo["TechnicianName"] as FieldUserValue).LookupId.ToString()).Value : string.Empty;
var workOrderStatus = wo["WorkOrderStatus"] != null ? wo["WorkOrderStatus"].ToString() : string.Empty;
var createdStr = wo["Created"] != null ? wo["Created"].ToString() : null;
DateTime.TryParse(createdStr, out DateTime created); // Defaults to DateTime.MinValue if parsing fails
var assetId = wo["AssetID"] != null ? wo["AssetID"].ToString() : null;
var mediumID = !string.IsNullOrEmpty(assetId) && AssetIdMediumMap.TryGetValue(assetId, out var mid) ? mid : string.Empty;
return new
{
TechnicianName = technicianName,
WorkOrderStatus = workOrderStatus,
Year = created.Year,
Month = created.Month,
MediumID = mediumID,
};
})
// Ensure that you're grouping by a composite key of all the fields
.GroupBy(wo => new { wo.TechnicianName, wo.Year, wo.Month, wo.MediumID })
.Select(g => new
{
TechnicianName = g.Key.TechnicianName,
Year = g.Key.Year,
Month = g.Key.Month,
MediumID = g.Key.MediumID,
TotalCount = g.Count(),
DoneCount = g.Count(wo => wo.WorkOrderStatus == "Done")
})
.ToList(); // Materialize the query to a list
now i am getting the totalcount and the DoneCount.. but we have 2 Media IDs (10 & 20) which have a weight of 1/2.. so, when counting those i need to consider that their weight on the totals is 1/2 while other media have a weight od 1. so how i can achieve this?
Thanks
Upvotes: 0
Views: 66
Reputation: 174
Similar to answer by @flackoverstow but using pattern matching.
var doneCount =
g.Where(wo => wo.WorkOrderStatus == "Done")
.Sum(
wo => wo switch
{
{ MediumId: 10 or 20 } => 0.5,
_ => 1
}
);
Upvotes: 1