Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 比较两个foreach循环中的两个值_C# - Fatal编程技术网

C# 比较两个foreach循环中的两个值

C# 比较两个foreach循环中的两个值,c#,C#,我有一个Foreach循环,每次迭代都将记录放入一个系统中。然后我还有一个foreach循环,它从linqQuery获取数据。我想遍历每一个,比较每一个的全名,如果它是真的,它会将bool标记为真,如果不是,它会将bool标记为假。我该怎么做呢。这就是我目前所拥有的 foreach (Lead p in sessionleads) { string AccountName = ""; string AccountId = "";

我有一个Foreach循环,每次迭代都将记录放入一个系统中。然后我还有一个foreach循环,它从linqQuery获取数据。我想遍历每一个,比较每一个的全名,如果它是真的,它会将bool标记为真,如果不是,它会将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
{
   ....
}

然后,使用它进行其余的逻辑检查。

在第一次检查后添加一个中断:

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);
}
第一遍的几个要点

  • 从不指定收缩的
    帐户ID
    (以小写开头)。我想它们是guid
  • 如果要立即重置变量,请不要不必要地初始化变量。就像你对
    AccountName
    AccountId
    等所做的那样
  • 不需要
    AccountName
    变量
  • 我保留了
    AccountName
    ContactId
    变量名,但大多数人以小写字母开始局部变量
  • 回到原来的问题。缺少一个
    中断
    ,一旦找到匹配项就退出循环
  • 干(不要重复自己)机会的大部分代码都是重复的,因为
    b==true
    b==false
    。尽量不要这样做,这会使维护变得困难。通过将代码位提取到一个小的、有重点的方法(如
    CreateOpportunity(…)
    )中,可以修复很多次的重复。不要害怕把方法变小
过二号。同样的免责声明

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
连接可以更进一步,但我认为在这一点上它非常紧密。我需要睡觉。希望这对你有用

简而言之,@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);
}
第一遍的几个要点

  • 从不指定收缩的
    帐户ID
    (以小写开头)。我想它们是guid
  • 如果要立即重置变量,请不要不必要地初始化变量。就像你对
    AccountName
    AccountId
    等所做的那样
  • 不需要
    AccountName
    变量
  • 我保留了
    AccountName
    ContactId
    变量名,但大多数人以小写字母开始局部变量
  • 回到原来的问题。缺少一个
    中断
    ,一旦找到匹配项就退出循环
  • 干(不要重复自己)
机会的大部分代码都是重复的,因为
b==true
b==false
。尽量不要这样做,这会使维护变得困难。通过将代码位提取到一个小的、有重点的方法(如
CreateOpportunity(…)
)中,可以修复很多次的重复。不要害怕把方法变小 过二号。同样的免责声明

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
连接可以更进一步,但我认为在这一点上它非常紧密。我需要睡觉。希望这对你有用

不如用
连接
而不是
连接,其中i.AccountName==p.AccountName
?这可能快得多。然而,这超出了我的预期。不如用
加入
而不是
其中I.AccountName==p.AccountName
?这可能快得多。然而,这超出了我的预期。