修复没有值的Foreach循环

本文关键字:Foreach 循环 | 更新日期: 2023-09-27 18:04:50

我正在尝试执行以下代码,其中在我的DB中充满游戏id的两列是匹配的,然后执行三个单独的查询以获取匹配的游戏id的相关信息,并且它都很好地打包在电子邮件中。

foreach (var TempId in ResultGameIDCompare)
var command1 = "EXEC DB query";
var command2 = "EXEC DB query";
var command3 = "EXEC DB query";
var GameInfo = db2.Query(command1, (string)TempId.Game_ID);
var SystemInfo = db2.Query(command2, (string)TempId.Game_ID); 
var EditionInfo = db2.Query(command3, (string)TempId.Game_ID);  
foreach (var result in GameInfo) 
      {
        foreach (var result2 in SystemInfo) 
         {
           foreach (var result3 in EditionInfo) 
             {
                var text = " <ul>"+
                             "<p>My game information is as follows:" +@result.Name+"</p>"+
                             " <strong>System:</strong> "   @result2.System+
                             " <strong>Price:</strong> "  +@result3.Price+
                             " <strong>Edition:</strong> "  +@result3.Edition+
                             "<ul>";
                 WebMail.Send("Test@test.com",
                   "This is a test", 
                   body : text, 
                   isBodyHtml:true );  
               }  
           }
       }  

代码可以工作,但是如果我的一个查询返回空行,则不会将任何内容馈送到该game id的集合中,并且可以理解的是,不会为该game id生成电子邮件。

所以我试图重新工作我的代码,所以电子邮件仍然生成,即使为SystemInfoEditionInfo查询返回空行(GameInfo永远不会返回空行),但我很难弄清楚如何做到这一点。我已经尝试修改我的代码如下:

var Blank = " ";
foreach (var TempId in ResultGameIDCompare)
var command1 = "EXEC DB query";
var command2 = "EXEC DB query";
var command3 = "EXEC DB query";
var GameInfo = db2.Query(command1, (string)TempId.Game_ID);
var SystemInfo = db2.Query(command2, (string)TempId.Game_ID); 
var EditionInfo = db2.Query(command3, (string)TempId.Game_ID);  
foreach (var result in GameInfo) 
      {
        foreach (var result2 in SystemInfo.DefaultIfEmpty(Blank)) 
         {
           foreach (var result3 in EditionInfo.DefaultIfEmpty(Blank)) 
             {
                var text = " <ul>"+
                             "<p>My game information is as follows:" +@result.Name+"</p>"+
                             " <strong>System:</strong> "   @result2.System+
                             " <strong>Price:</strong> "  +@result3.Price+
                             " <strong>Edition:</strong> "  +@result3.Edition+
                             "<ul>";
                 WebMail.Send("Test@test.com",
                   "This is a test", 
                   body : text, 
                   isBodyHtml:true );  
               }  
           }
       }    

但是它返回错误"string' does not contain a definition for 'System'",我真的不确定这种改变是否会做我认为它会做的事情。在查询没有返回数据的位置返回" "。

修复没有值的Foreach循环

foreach (var result in GameInfo)
{
    StringBuilder sb = new StringBuilder();
    sb.Append("<ul>");
    sb.AppendFormat("<p>My game information is as follows: {0}</p>", @result.Name);
    foreach (var result2 in SystemInfo)
    {
        sb.AppendFormat("<strong>System: {0}</strong>", @result2.System);
        foreach (var result3 in EditionInfo)
        {
            sb.AppendFormat("<strong>Price: {0}</strong>", @result3.Price);
            sb.AppendFormat("<strong>Edition: {0}</strong>", @result3.Edtion);
        }
    }
    sb.Append("</ul>");
    WebMail.Send("Test@test.com",
      "This is a test",
      body: sb.ToString(),
      isBodyHtml: true);  
}

只需将字符串的创建移到第一个循环中,并在获得更多数据时在内部循环中添加它。StringBuilderSystem.Text命名空间中的高性能字符串连接系统,避免了昂贵的+连接。

您的变量Blank需要是与原始集合相同类型的IEnumerable

如果你的SystemInfo是一个字符串的集合,

SystemInfo.DefaultIfEmpty(new[]{ Blank })