[英]how to check for where in Linq C#
以下是我的代碼,我不希望Taskbugsworkitem中包含值為0的Storyid。
但是我選擇時正在檢查故事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
的結果,所以您想使用w.WorkItemLinks.Any()
在Where()
過濾掉這些記錄。
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();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.