user482375
user482375

Reputation:

Comparing two values from two foreach loops

I have a Foreach loop and every iteration puts the record into a system. Then I also have a foreach loop that gets the data from a linqQuery. I want to iterate through each one and compare the FullName of each and if its true it makes a bool as true and if not it marks it as false. How would I go about doing that. This is what I have so far.

    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);
        }

    }

Thanks!

Upvotes: 0

Views: 3882

Answers (2)

codeConcussion
codeConcussion

Reputation: 12938

The short answer - @Ingenu is right. You're just missing a break keyword. However, you might want to consider some other refactorings to tighten the code up a bit.

Here's a first pass, keeping roughly the same structure. Disclaimer: this was coded in notepad - not guaranteed to even compile...

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);
}

A few key points from the first pass...

  • contractId and accountId (starting with a lower-case) are never specified. I assume they are guids.
  • Don't uneccessarily initialize variables if you are just going to reset them immediately. Like you do with AccountName, AccountId, etc.
  • The AccountName variable isn't needed.
  • I left the AccountName and ContactId variable names as they were, but most people start local variables with a lower-case letter.
  • Back to the original question. Missing a break to exit the loop once a match is found.
  • DRY (Don't Repeat Yourself) - most of the opportunity code is duplicated for b == true and b == false. Try not to do that, it makes maintenance hard. A lot of times duplication can be fixed by extracting bits of code out into a small, focused method, like CreateOpportunity(...). Don't be afraid to make methods small.

Pass number two. Same disclaimer...

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);
}
  • Got rid of the inner for loop with a Linq extension method. Linq extension methods - learn them, love them, they are your best friend.

It could be taken a step further with a Linq Join, but I think it's pretty tight at this point. That and I need sleep. Hope this works for you!

Upvotes: 1

Roland Mai
Roland Mai

Reputation: 31077

Add a break after the first check:

if (AccountName.ToString() == p.AccountName.ToString())
{
    b = true;
    break;
    ...

Even better:

var item = (from i in linqQuery 
            where i.AccountName == p.AccountName 
            select i).FirstOrDefault();
bool found = (item != null);
if(found)
{
   ....
}
else
{
   ....
}

Then, the use that to do the rest of the logic checks.

Upvotes: 2

Related Questions