比较两个foreach循环的两个值
本文关键字:两个 foreach 比较 循环 | 更新日期: 2023-09-27 18:10:39
我有一个Foreach循环,每次迭代都将记录放入系统中。然后我还有一个foreach循环,从linqQuery获取数据。我想遍历每个并比较每个的FullName,如果它为真,它使bool为真,如果不是,它将其标记为假。我该怎么做呢?这是我目前掌握的信息。
foreach (Lead p in sessionleads)
{
string AccountName = "";
string AccountId = "";
string ContactId = "";
bool b = false;
foreach (var a in linqQuery)
{
AccountName = a.AccountName.ToString();
AccountId = a.AccountId.ToString();
ContactId = a.ContactId.ToString();
if (AccountName.ToString() == p.AccountName.ToString())
{
b = true;
}
else
{
b = false;
}
}
if (b == true)
{
Entity opportunity = new Entity("opportunity");
opportunity["new_contact"] = new EntityReference("contact", new Guid(ContactId));
opportunity["customerid"] = new EntityReference("account", new Guid(AccountId));
opportunity.FormattedValues["new_leadstatus"] = p.Status;
opportunity.FormattedValues["statuscode"] = p.Type;
//opportunity["ownerid"] =
Guid opportunityId = orgService.Create(opportunity);
}
else
{
Entity opportunity = new Entity("opportunity");
opportunity["new_contact"] = new EntityReference("contact", contactId);
opportunity["customerid"] = new EntityReference("account", accountId);
opportunity.FormattedValues["new_leadstatus"] = p.Status;
opportunity.FormattedValues["statuscode"] = p.Type;
//opportunity["ownerid"] =
Guid opportunityId = orgService.Create(opportunity);
}
}
谢谢!
在第一次检查后添加换行符:
if (AccountName.ToString() == p.AccountName.ToString())
{
b = true;
break;
...
更好:
var item = (from i in linqQuery
where i.AccountName == p.AccountName
select i).FirstOrDefault();
bool found = (item != null);
if(found)
{
....
}
else
{
....
}
然后,用它来做剩下的逻辑检查
简短的回答- @Ingenu是对的。你只是缺少了一个break
关键字。但是,您可能需要考虑一些其他重构来收紧代码。
这是第一次传递,保持大致相同的结构。免责声明:这是在记事本中编写的-不能保证甚至可以编译…
foreach (var p in sessionLeads)
{
Guid AccountId = accountId;
Guid ContactId = contractId;
foreach (var a in linqQuery)
{
if (a.AccountName == p.AccountName)
{
AccountId = new Guid(a.AccountId);
ContactId = new Guid(a.ContactId);
break;
}
}
Entity opportunity = new Entity("opportunity");
opportunity["new_contact"] = new EntityReference("contact", ContactId);
opportunity["customerid"] = new EntityReference("account", AccountId);
opportunity.FormattedValues["new_leadstatus"] = p.Status;
opportunity.FormattedValues["statuscode"] = p.Type;
orgService.Create(opportunity);
}
第一次传递的几个关键点…
-
contractId
和accountId
(以小写字母开头)从不指定。我猜它们是向导。 - 如果你只是想立即重置变量,不要不必要地初始化它们。就像你对
AccountName
,AccountId
等做的那样。 不需要 - 我保留了
AccountName
和ContactId
变量名,但是大多数人用小写字母开始局部变量。回到最初的问题。缺少一个break
来在找到匹配后退出循环。 - DRY (Don't Repeat Yourself) -
opportunity
的大部分代码在b == true
和b == false
中是重复的。尽量不要这样做,这会使维护变得困难。很多时候,复制可以通过将代码位提取到一个小的、集中的方法(如CreateOpportunity(...)
)中来修复。不要害怕让方法变小。
AccountName
变量。通过第二个。同样的免责声明…
foreach (var p in sessionLeads)
{
var match = linqQuery.FirstOrDefault(x => x.AccountName == p.AccountName);
Guid AccountId = (match != null ? new Guid(match.AccountId) : accountId);
Guid ContactId = (match != null ? new Guid(match.ContractId) : contractId);
Entity opportunity = new Entity("opportunity");
opportunity["new_contact"] = new EntityReference("contact", ContactId);
opportunity["customerid"] = new EntityReference("account", AccountId);
opportunity.FormattedValues["new_leadstatus"] = p.Status;
opportunity.FormattedValues["statuscode"] = p.Type;
orgService.Create(opportunity);
}
- 用Linq扩展方法去掉了内部的
for
循环。Linq扩展方法-学习他们,爱他们,他们是你最好的朋友。
它可以采取进一步与Linq Join
,但我认为它在这一点上是相当紧密的。还有,我需要睡觉。希望这对你有用!