【问题标题】:How can i couple item inside a loop?我怎样才能在一个循环中耦合项目?
【发布时间】:2014-02-13 16:12:33
【问题描述】:

我想在一个循环中耦合并显示一次两个项目?我尝试如下但我没有完成,因为指定的项目显示两次。

string item1 = null, item2 = null;
foreach(var item in myCollection)
{
    if(item.Id == 20)
    {
        item1 = item.Value;
    }
    else if(item.Id == 21)
    {
        item2 = item.Value;
    }

    if(item.Id != 20 && item.Id != 21)
    {
        //adding
    }
    else
    {
        if(item.Id !=null && item.Id != null)
        {
            myValue = item1 + item2;
            //This case is normaly because item.Id everytime is full.
            //I tried like this (item.Id == 20 && item.Id == 21) but i don't find any solution. 
        }
    }
} 

【问题讨论】:

  • 我不明白你到底想做什么。您是想从集合中消除重复项,还是将数字相加,还是什么?
  • 我想将两个项目合并为我上面的评论。
  • “合并”是什么意思? item 代表什么?
  • 如果你用文字一步一步地解释算法,也许会有所帮助,因为“结合两个项目”对我们来说太模糊了,我们无法理解。
  • try (item.Id == 20 || item.Id == 21) 因为如果你使用 && 它永远不会是真的,因为你在一个循环中

标签: c# loops if-statement foreach


【解决方案1】:

假设:

  1. 你有一个定义为{ int Id, string Value }的项目集合,

  2. 你想找到Ids 和2021 的两个项目,

  3. 您想将它们的字符串值连接成一个新字符串,

您可以简单地使用 LINQ:

var myValue = "";

// find the item with Id == 20 (if it exists)
var item1 = myCollection.FirstOrDefault(i => i.Id == 20);

// find the item with Id == 21 (if it exists)
var item2 = myCollection.FirstOrDefault(i => i.Id == 21);

// if both items are found, join their values into a new string
if (item1 != null && item2 != null)
    myValue = item1.Value + item2.Value;

对于更一般的问题(“连接具有特定 ID 的所有项目”),您可以将其重写为:

// list of all IDs you want to find
var idsToFind = new[] { 20, 21, 22, 23 };

// find items with corresponding IDs
var items = myCollection.Where(i => idsToFind.Contains(i.Id));

// concatenate results
var myValue = string.Concat(items);

【讨论】:

    【解决方案2】:

    最后一个else分支好像有错别字:

    if (item.Id !=null && item.Id != null)
    

    您可能想检查 item1 和 item2 是否为空?

    【讨论】:

      【解决方案3】:

      如果通过组合你的意思是连接字符串,那么试试这个

      string item1 = null, item2 = null,myValue = "";
      foreach(var item in myCollection)
      {
          if(item.Id == 20)
          {
              item1 = item.Value;
          }
          else if(item.Id == 21)
          {
              item2 = item.Value;
          }
      
          if(item.Id != 20 && item.Id != 21)
          {
              //adding
          }
          else
          {
              if(item.Id !=null)
              {
                  myValue = myValue + item1 + item2;
      
              }
          }
      } 
      

      【讨论】:

      • 这行不通。首先,myValue 最初为 null,访问它会抛出 NullReferenceException。接下来,(item.Id !=null && item.Id != null) 可以简单地写成(item.Id !=null),但更可能是一个错字,应该是(item1 !=null && item2 != null)。没有其他意义,因为Id 几乎可以肯定是普通的int 并且不能为空,因此 OP 希望在连接它们之前检查 item1item2 的字符串值是否为非空。
      【解决方案4】:

      很难说你到底想要达到什么目标。如果您提供更多详细信息,我会相应地更新答案。

      我猜你有问题:

      (item.Id == 20 && item.Id == 21)
      

      这在任何“正常”情况下都不会评估为 true,因为 item.Id 不可能同时为 20 和 21。
      (在某些“特殊”情况下,另一个线程可以在第一次比较后立即将 item.Id 从 20 更新到 21,以便该行的计算结果为 true)。

      也许您需要使用|| 而不是&&

      (item.Id == 20 || item.Id == 21)
      

      【讨论】:

      • @Gigi 我说的是他的评论,他说'我试过这样'
      • 对,没看到。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-10-25
      • 2020-08-14
      • 1970-01-01
      • 2013-08-18
      • 1970-01-01
      • 2020-08-24
      • 1970-01-01
      相关资源
      最近更新 更多