如何检查Linq C#的位置

问题描述 投票:0回答:3

下面是我的代码,我希望不要将其值为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();
c# .net linq
3个回答
2
投票

如果你想获得只有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();

0
投票

试试这个。

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();

0
投票

如果我正确地读了你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();
© www.soinside.com 2019 - 2024. All rights reserved.