简体   繁体   English

检查foreach循环列表结果C#

[英]Checking foreach loop list results C#

I have this bit of code in a class: 我在课堂上有这段代码:

public class TicketSummary
    {

        //get all the development tickets
        public List<IncidentSummary> AllDevelopmentTickets { get; set; }
      public List<string> TicketNames()
        {
            List<string> v = new List<string>();

            foreach (var developmentTicket in AllDevelopmentTickets)
            {
                var ticketIds = developmentTicket.id.ToString(CultureInfo.InvariantCulture);
                v.Add(ticketIds);
            }

            return v;
        }  
    }
}

And I am trying to see if my API connection (plus all the code) did it's job and pulls back the tickets and their info, more specifically the ids. 我正在尝试查看我的API连接(以及所有代码)是否完成了工作,并拉回了票证及其信息,更具体地说是id。

In my main program I have no clue how to check if it did the job. 在我的主程序中,我不知道如何检查它是否完成了工作。 I tried something but it isn't quite right and doesn't return anything ( I know I need a Console.WriteLine) 我尝试了一些东西,但是它不太正确,并且没有返回任何东西(我知道我需要一个Console.WriteLine)

        static void Main(string[] args)
        {

            Console.ReadLine();


            var tickets = new TicketSummary();
            tickets.TicketNames();
            while ( tickets != null )
            {
                Console.WriteLine(tickets);
            }

        }

Any suggestions, please? 有什么建议吗?

Thank you! 谢谢!

You've dropped the returned result: tickets.TicketNames(); 您删除了返回的结果: tickets.TicketNames(); returns List<String> that you have to assign and then itterate : 返回必须分配的 List<String> ,然后执行itterate

 var tickets = new TicketSummary();
 var names = tickets.TicketNames(); // <- names, List<String> according to the code

 // printing out all the names
 foreach(var name in names)   
   Console.WriteLine(name);

Do you mean you just want to print all the tickets out? 您是说只想打印出所有票吗?

foreach (var ticket in tickets.TicketNames())
{
    Console.WriteLine(ticket);
}

You have several problems in your code, that should keep it from even compiling, but aside from that, It seem's what you're really after is rather transforming the data in AllDevelopmentTickets , rather than moving it somewhere. 您的代码中有几个问题,应该阻止甚至编译,但除此之外,您似乎真正想要的是在AllDevelopmentTickets 转换数据,而不是数据移到某个地方。 So you could probably do it with a Select call (from LINQ). 因此,您可以通过Select调用(来自LINQ)来实现。 So, in your main method: 因此,在您的主要方法中:

var tickets = new TicketSummary();
// add some tickets to tickets.AllDevelopmentTickets here...

var ticketNames = tickets.AllDevelopmentTickets.Select(ticket => ticket.id.ToString();
// Yes, you should probably use an UI culture in the ToString call. 
// I'm just trying to limit my line width =)

Now, ticketNames should be an IEnumerable<string> holding all the ticket ids. 现在, ticketNames应该是一个IEnumerable<string>所有票证ID。 To, for example, print them out, you can iterate over them and write to console output: 例如,要打印出它们,可以遍历它们并写入控制台输出:

foreach (var name in ticketNames) {
    Console.WriteLine(name);
}

You're ignoring the returned value. 您将忽略返回的值。

static void Main(string[] args)
    {

        Console.ReadLine();


        var tickets = new TicketSummary();
        var res = tickets.TicketNames();
        while ( for r in res )
        {
            Console.WriteLine(r);
        }

    }

You need to assign / use the return value of the TicketNames() method. 您需要分配/使用TicketNames()方法的返回值。 This seems a lot of work just to return the string version of the TicketId. 只是为了返回TicketId的字符串版本,这似乎有很多工作。 This can be reduced 这可以减少

public List<string> TicketNames()
{
    return AllDevelopmentTickets
        .Select(t => t.id.ToString(CultureInfo.InvariantCulture))
        .ToList();
}  

   var ticketSummary = new TicketSummary();
   var ticketNames = ticketSummary.TicketNames();

    foreach(var ticketName in ticketNames)
    {
        Console.WriteLine(ticketName);
    }

or Even just: 甚至:

  foreach(var ticketName in AllDevelopmentTickets
        .Select(t => t.id.ToString(CultureInfo.InvariantCulture)))
  {
      Console.WriteLine(ticketName);
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM