下面是我的代码,我希望不要将其值为0的Storyid包含在taskbugsworkitem中。 但我正在选择时检查故事ID值。我还在学习linq需要帮助
var taskbugsworkitem =
(from w in workItemcollectionList where (w.Type.Name == "Task") || w.Type.Name == "Bug" select new {
Id = w.Id,
Name = w.Title,
Type = w.Type.Name,
Storyid = (w.WorkItemLinks.Count > 0) ? w.WorkItemLinks[0].TargetId : 0,
status = w.State,
IterationPath = w.IterationPath,
Assignedto = w.Fields["Assigned To"].Value.ToString(),
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
effort = Convert.ToInt32(w.Fields["effort"].Value),
Completed = (w.Type.Name== "Task") ? Convert.ToInt32(w.Fields["Completed"].Value):0
}) .ToList();
如果你想获得只有WorkItemLinks的taskbugworkitem
,你可以像这样查询:
var taskbugsworkitem =
(from w in workItemcollectionList
where (w.Type.Name == "Task" || w.Type.Name == "Bug")
&& w.WorkItemLinks?.Count > 0
select new
{
Id = w.Id,
Name = w.Title,
Type = w.Type.Name,
Storyid = w.WorkItemLinks[0].TargetId ,
status = w.State,
IterationPath = w.IterationPath,
Assignedto = w.Fields["Assigned To"].Value.ToString(),
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
effort = Convert.ToInt32(w.Fields["effort"].Value),
Completed = (w.Type.Name == "Task") ? Convert.ToInt32(w.Fields["Completed"].Value) : 0
}).ToList();
试试这个。
var taskbugsworkitem =
(from w in workItemcollectionList where (w.Type.Name == "Task") || w.Type.Name == "Bug" && w.Storyid!=0 select new {
Id = w.Id,
Name = w.Title,
Type = w.Type.Name,
Storyid =w.Storyid,
status = w.State,
IterationPath = w.IterationPath,
Assignedto = w.Fields["Assigned To"].Value.ToString(),
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
effort = Convert.ToInt32(w.Fields["effort"].Value),
Completed = (w.Type.Name== "Task") ? Convert.ToInt32(w.Fields["Completed"].Value):0
}) .ToList();
如果我正确地读了你StoryId == 0
完全是w.WorkItemLinks.Count ==0
的结果,那么你想使用Where()
在w.WorkItemLinks.Any()
中过滤掉这些记录。
var taskbugsworkitem = workItemcollectionList
.Where((w=>w.Type.Name == "Task" || w.Type.Name == "Bug")&& w.WorkItemLinks.Any())
.Select(w=> new{
Id = w.Id,
Name = w.Title,
Type = w.Type.Name,
Storyid = w.WorkItemLinks[0].TargetId,
status = w.State,
IterationPath = w.IterationPath,
Assignedto = w.Fields["Assigned To"].Value.ToString(),
priorty = Convert.ToInt32(w.Fields["Priority"].Value),
effort = Convert.ToInt32(w.Fields["effort"].Value),
Completed = (w.Type.Name == "Task") ? Convert.ToInt32(w.Fields["Completed"].Value) : 0 })
.ToList();