How to get the fields from a WorkItem using a WIQL in one call to speed up? - performance

I've have the following code to get some specific WorkItems:
string workItemQueryString = "Select Id, State, Type From WorkItems Where [Work Item Type] = 'Code Review Request' And [Area Path] = 'abc' Order By [Changed Date] Desc";
var workItemQuery = new Query(workItemStore, workItemQueryString);
WorkItemCollection queryResults = workItemQuery.RunQuery();
This code runs fast (< 1 sec). However I also want to get some extra fields like "Associated Context Type" and "Associated Context".
So I use this code get get those fields:
var workItemDetails = queryResults.Cast<WorkItem>().Select(workItem => new WorkItemDetail
{
WorkItem = workItem,
AssociatedContextType = workItem.Fields.Contains("Associated Context Type") ? workItem.Fields["Associated Context Type"].Value : null,
AssociatedContext = workItem.Fields.Contains("Associated Context") ? workItem.Fields["Associated Context"].Value : null
}).ToList();
But this code runs very slow (13 to 20 seconds) which looks to me that separate queries (for each workitem?) are fired to the TFS server to get all data.
Note that when I use a Parallel.ForEach statement, the code breaks with an exception.
The total number of WorkItems in the WorkItemCollection is about 2800.

Try to change:
AssociatedContextType = workItem.Fields.Contains("AssociatedContextType") ? workItem.Fields["AssociatedContextType"].Value : null,
AssociatedContext = workItem.Fields.Contains("AssociatedContext") ? workItem.Fields["AssociatedContext"].Value : null
to:
AssociatedContextType = workItem.Fields.Contains("Associated Context Type") ? workItem.Fields["Associated Context Type"].Value : null,
AssociatedContext = workItem.Fields.Contains("Associated Context") ? workItem.Fields["Associated Context"].Value : null

Related

power query Expression.Error: We cannot convert a value of type Function to type List in power query

I am trying to run the query in power query editor and it failed with below error
where as the same query works fine in winserver 2016 and it fails with above error in win server 2019. Am I missing any thing ?
I did compared the settings and everything looks good.
let
//Get data Report H1
Source1 = Excel.Workbook(File.Contents("\\path\filename.xlsx"), null, true),
#"classic_Sheet1" = Source1{[Item = "classic", Kind = "Sheet"]}[Data],
#"Trimmed Text1" = Table.TransformColumns(#"classic_Sheet1", Text.Trim),
#"Third Row as Header1" = Table.PromoteHeaders(Table.Skip(#"Trimmed Text1", 2)),
#"Selected Columns1" = Table.SelectColumns(
#"Third Row as Header1",
{" ID", " Status", "Customer Id ", "Agent", "Leg"}
),
//Get Report H2
Source2 = Excel.Workbook(File.Contents("\\path\filename.xlsx"), null, true),
#"classic_Sheet2" = Source2{[Item = "classic", Kind = "Sheet"]}[Data],
#"Trimmed Text2" = Table.TransformColumns(#"classic_Sheet2", Text.Trim),
#"Third Row as Header2" = Table.PromoteHeaders(Table.Skip(#"Trimmed Text2", 2)),
#"Selected Columns2" = Table.SelectColumns(
#"Third Row as Header2",
{" ID", "Status", "Customer Id ", "Agent", "Leg"}
)
in
#"Excluded IDs"
The error message is pointing you to the problem:
Table.TransformColumns expects a list as the second parameter,
while you are providing a function:
Table.TransformColumns(
table as table,
transformOperations as list,
optional defaultTransformation as nullable function,
optional missingField as nullable number
) as table
Please read the official documentation here:
https://learn.microsoft.com/en-us/powerquery-m/table-transformcolumns
The issue has nothing to do with winserver 2016 or winserver 2019.
You want something along these lines
= Table.TransformColumns(#"classic_Sheet1",{{"ColumnNameHere", Text.Trim, type text}})
= Table.TransformColumns(#"classic_Sheet1"",{{"ColumnNameHere", Text.Trim, type text}, {"DifferentColumnNameHere", Text.Trim, type text}})

first_or_initialize is not working for relative model and creating new record each time

ccs.each do |cd|
relative_model = main_model.relative_model.where(start_date: XYZ, end_date: XYZ).first_or_initialize
relative_model.capacity = cd['capacity'].to_f
relative_model.save!
end
As per above code, first_or_initialize is not working for relative model and creating new record each time.
Here is the query run at background for both inr:
SELECT `capacity_commitments`.* FROM `capacity_commitments` WHERE `capacity_commitments`.`participants_subscription_id` = 1 AND `capacity_commitments`.`start_date` = '2016-11-16' AND `capacity_commitments`.`end_date` = '2016-11-21'
SELECT `capacity_commitments`.* FROM `capacity_commitments` WHERE `capacity_commitments`.`participants_subscription_id` = 1 AND `capacity_commitments`.`start_date` = '2016-11-16' AND `capacity_commitments`.`end_date` = '2016-11-21'
NEED INITIAL HELP OR POINT OUT WHAT IS WRONG IN ABOVE CODE?
Try to use find_or_initialize_by method
relative_model = main_model.relative_model.find_or_initialize_by(start_date: XYZ, end_date: XYZ)

Calculating time difference in sapui5

Good day,
Disclaimer: I am very new to development.
I am developing an app and the user needs to enter a start time and end time. I then want to do a calculation to get the time taken. Below is the code I am using but it returns "an empty string".
dtiStartTime = new sap.m.DateTimeInput({
id : "StartTime", placeholder:"Select Start Time", type:"Time",width:"100px"
});
dtitotalTime = new sap.m.DateTimeInput({type:"Time",value:""});
dtiStopTime = new sap.m.DateTimeInput({
id : "StopTime", placeholder:"Select Stop Time", type:"Time",width:"100px",
change: [function(oEvent){totalTime.setValue(dtiStopTime.getValue() - dtiStartTime.getValue())}]
});
"getValue()" of DateTimeInput returns a javascript Date() object.
If you call "getTime()" of the Date object you get the time in milliseconds and can subtract them from each other. Afterwards you need to transform them into a value that you need (minutes/hours)...
var startDate = dtiStartTime.getValue();
var stopDate = dtiStopTime.getValue();
var differenceInMilliseconds = stopDate.getTime() - startDate.getTime();
Here is an example how to get the difference in seconds:
var start = this.byId('dateTimeStart').getDateValue();
var ende = this.byId('dateTimeEnde').getDateValue();
var diffInSeconds = (ende.getTime() - start.getTime())/1000;

LINQ Subquery issue

Following is my linq query:
var varResourceStatusReportDataBase =
(
from content in listContent
join workflowInstance in listWorkflkowInstance
on content.Field<string>("ows_ID").Trim()
equals workflowInstance.Field<string>("ows_Content ID").Split(';')[0].Trim()
join WorkflowInstanceStep in listWorkflowInstanceStep
on workflowInstance.Field<string>("ows_ID")
equals WorkflowInstanceStep.Field<string>("ows_Workflow Instance ID").Split(';')[0]
select new
{
ContentName = content.Field<string>("ows_Name"),
WorkflowInstanceId = workflowInstance.Field<string>("ows_ID"),
WIName = workflowInstance.Field<string>("ows_Title"),
WIPlannedStartDate = workflowInstance.Field<string>("ows_Planned Start Date") ?? "",
WIPlannedEndDate = workflowInstance.Field<string>("ows_Planned End Date") ?? "",
WIActualStartDate = workflowInstance.Field<string>("ows_Actual Start Date") ?? "",
WIActualEndDate = workflowInstance.Field<string>("ows_Actual End Date") ?? "",
WIApprovalDate = workflowInstance.Field<string>("ows_Approval Date") ?? "",
WITaskStatus = workflowInstance.Field<string>("ows_Status").ToUpper() ?? "",
WIMetadataStatus = workflowInstance.Field<string>("ows_Metadata Status") ?? "",
WIApprover = workflowInstance.Field<string>("ows_Approver").Replace("#", "").Split(';')[1].ToUpper() ?? "",
WISResponsible = WorkflowInstanceStep.Field<string>("ows_Responsible").Replace("#", "").Split(';')[1].ToUpper() ?? "",
WISDesiredEndDate=
(
from WorkflowInstanceStep1 in listWorkflowInstanceStep
where (WorkflowInstanceStep1.Field<string>("ows_Status") =="IN PROGRESS" ||
WorkflowInstanceStep1.Field<string>("ows_Status") =="ASSIGNED") &&
workflowInstance.Field<string>("ows_ID") == WorkflowInstanceStep1.Field<string>("ows_Workflow Instance ID").Split(';')[0]
select new {abc = WorkflowInstanceStep1.Field<string>("ows_Desired End Date")}).Take(1)
}).Distinct();
I have sub-query in above query to calculate WISDesiredEndDate, but when I execute this query, I get System.Linq.Enumerable+TakeIterator>d__3a'1[<>f__AnonymousType1'1[System.String]] value for WISDesiredEndDate column not actual value which I want.
Please give suggestion on the same.
Thanks.
You want FirstOrDefault() rather than Take(1)
As from comments, you don't want a anonymous object so change the select to be
select WorkflowInstanceStep1.Field<string>("ows_Desired End Date")}).FirstOrDefault()
You can us let clause more readable code. Nevertheless you have to force linq expression execution by calling FirstOrDefault method
let desiredEndDate = (from WorkflowInstanceStep1 in listWorkflowInstanceStep .... Take(1))
select new
{
...
WISDesiredEndDate= desiredEndDate.FirstOrDefault()
}

Doing Sum() on a set of anonymous type: is it possible?

If I select an anonymous type (select new) from a query:
var smsPerGroup = from g in db.Groups
select new
{
GroupName = g.Name,
ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()
};
It happends that I can't sum() on it:
int max = smsPerGroup.Max(g => g.ReceivedSMS); // ERROR!
The error thrown is:
There was an error parsing the query.
What is the most correct solution for this?
I don't want to create a class to use ONLY one time, on this query.
Is it possible to have a "not very anonymous" type? this is: to define the type of its properties but not the type of the class itself
Something like this: (which is syntactically incorrect)
...
select new
{
string GroupName = g.Name,
int ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()
};
Edit: the error in detail is the following
There was an error parsing the query. [ Token line number = 6,Token line offset = 5,Token in error = SELECT ]
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.Data.SqlServerCe.SqlCeException: There was an error parsing the query. [ Token line number = 6,Token line offset = 5,Token in error = SELECT ]
Source Error:
Line 159: };
Line 160:
Line 161: int max = smsPerGroup.Max(g => g.ReceivedSMS);
A simple solution would be to materialize the query with .ToList() before calling .Sum():
var test = (from g in db.Groups
select new
{
GroupName = g.Name,
ReceivedSMS = g.Members.SelectMany(p => p.ReceivedSMS).Count()
}).ToList();
var sum = test.Sum(t => t.ReceivedSMS);

Resources