Click here to Skip to main content
16,017,788 members
Please Sign up or sign in to vote.
2.50/5 (2 votes)
See more:
Hi,
I am new to LinQ, i want to convert a foreach with if else statement into linq,
please help me out thanks in advance
below is my code

C#
foreach (Contact contactValues in contactsFromDb)
                {
                    Outlook.MAPIFolder contacts = Globals.ThisAddIn.Application.Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
                    Outlook.Items colItems = contacts.Items;
                    string contactId = contactValues.Id.ToString();
                    string FilterString = "[CustomerID] = '" + contactId + "'";
                    Outlook.ContactItem excistingContact = (Outlook.ContactItem)colItems.Find(FilterString);
                    if (excistingContact == null)
                    {
                        Outlook.ContactItem oContact = (Outlook.ContactItem)contacts.Items.Add(Common.Helper.ContactClassForm);
                        AssignvaluesToContact(contactValues, null, contacts);
                        i++;
                    }
                    else
                    {
                        if (excistingContact.LastModificationTime < contactValues.LastModifiedDate)
                        {
                            Outlook.ContactItem oContact = (Outlook.ContactItem)contacts.Items.Add(Common.Helper.ContactClassForm);
                            AssignvaluesToContact(contactValues, excistingContact, contacts);
                        }
                    }
                }
Posted
Comments
dan!sh 11-Jan-16 7:06am    
What is the data type of contactsFromDb? Regardless, I do not think you will gain anything by converting this to LINQ expression. This is actually better in terms of readability IMHO.

1 solution

as you said LINQ may not help for performance but i would still like to make a try with LinQ just to see how it behaves with LINQ , it would be great if you can help. thanks
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900