C# 如何在循环中耦合项目?
我想在一个循环中耦合并显示两个项目一次?我尝试了如下操作,但没有完成,因为指定的项目显示了两次C# 如何在循环中耦合项目?,c#,loops,if-statement,foreach,C#,Loops,If Statement,Foreach,我想在一个循环中耦合并显示两个项目一次?我尝试了如下操作,但没有完成,因为指定的项目显示了两次 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; }
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.Id == 20 && item.Id == 21)
这在任何“正常”情况下都不会计算为true,因为item.Id不可能同时为20和21。(有一些“异国情调”的情况下,另一个线程可以在第一次比较之后立即将item.Id从20更新到21,以便此行的计算结果为true) 也许您需要使用
|
而不是&
:
(item.Id == 20 || item.Id == 21)
很难说你到底想达到什么目的。如果你提供更多细节,我会相应地更新答案 我猜你在以下方面有问题:
(item.Id == 20 && item.Id == 21)
这在任何“正常”情况下都不会计算为true,因为item.Id不可能同时为20和21。(有一些“异国情调”的情况下,另一个线程可以在第一次比较之后立即将item.Id从20更新到21,以便此行的计算结果为true) 也许您需要使用
|
而不是&
:
(item.Id == 20 || item.Id == 21)
假定:
{int-Id,string-Value}
的项集合s的项目,分别为20
和21
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);
假定:
{int-Id,string-Value}
的项集合s的项目,分别为20
和21
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);
如果combine的意思是连接字符串,那么试试这个
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;
}
}
}
如果combine的意思是连接字符串,那么试试这个
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;
}
}
}
最后一个else分支中似乎有输入错误:
if (item.Id !=null && item.Id != null)
您可能想检查item1和item2是否为空?最后一个else分支中似乎有输入错误:
if (item.Id !=null && item.Id != null)
可能您想检查item1和item2是否为空?我不明白您到底想做什么。你是在试图从一个集合中消除重复项,还是将数字相加,还是什么?我想将两个项目合并为我上面的注释。“合并”是什么意思?一个
item
代表什么?如果你用文字一步一步地解释这个算法也许会有帮助,因为“合并两个item”对我们来说太模糊了,无法理解。你是在试图从一个集合中消除重复项,还是将数字相加,还是什么?我想将两个项目合并为我上面的注释。“合并”是什么意思?item
代表什么?如果你用文字一步一步地解释这个算法,也许会有帮助,因为“合并两个item”对我们来说太模糊了,无法理解。试试(item.Id==20 | | item.Id==21)因为如果你使用&&它将永远不会是真的,因为你在一个loop@Gigi我是说他的评论,,他说‘我这样试过’对,没看到。@Gigi我在谈论他的评论,他说‘我这样试过’对,没看到。这行不通。首先,myValue
最初为null,访问它将引发NullReferenceException
。接下来,(item.Id!=null和&item.Id!=null)
可以简单地写为(item.Id!=null)
,但更可能是一个打字错误,应该是(item1!=null和&item2!=null)
。由于Id
几乎肯定是一个普通的int
并且不能为null,因此OP希望在连接它们之前检查item1
和item2
的字符串值是否为非null。这不起作用。首先,myValue
最初为null,访问它将引发NullReferenceException
。接下来,(item.Id!=null和&item.Id!=null)
可以简单地写为(item.Id!=null)
,但更可能是一个打字错误,应该是(item1!=null和&item2!=null)
。由于Id
几乎肯定是一个普通的int
且不能为空,因此OP希望在连接item1
和item2
之前检查字符串值是否为非空。