将foreach转换为Linq语句 [英] convert foreach with looping into Linq statement
本文介绍了将foreach转换为Linq语句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是LinQ的新手,我想用if else语句将foreach转换成linq,
请提前帮助我谢谢
以下是我的代码
foreach (联系方式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);
}
}
}
解决方案
正如你所说LINQ可能对性能没有帮助但是我我仍然想尝试使用LinQ来看看它是如何与LINQ一起运行的,如果你可以帮忙的话会很棒。感谢
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
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);
}
}
}
解决方案
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
这篇关于将foreach转换为Linq语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文