我如何使用 OR 條件鏈接 EF 查詢
現在我正在鏈接或構建如下查詢,這最終添加了 AND 條件
if (model.EMailChoices?.Count() > 0)
{
query = query.Where(
c => model.EMailChoices.Contains(c.Contact.CommunicationPreferences.TPEMail)
);
}
if (model.MailChoices?.Count() > 0)
{
query = query.Where(
c => model.MailChoices.Contains(c.Contact.CommunicationPreferences.TPMail)
);
}
if (model.PhoneChoices?.Count() > 0)
{
query = query.Where(
c => model.PhoneChoices.Contains(c.Contact.CommunicationPreferences.TPTelephone)
);
}
我們如何在這個鏈中添加 OR 條件
uj5u.com熱心網友回復:
bool anyEmails = model.EMailChoices?.Any() == true;
bool anyMails = model.MailChoices?.Any() == true;
bool anyPhones = model.PhoneChoices?.Any() == true;
if(anyEmails || anyMails || anyPhones)
{
query = query.Where(
c => (anyEmails && model.EMailChoices.Contains(c.Contact.CommunicationPreferences.TPEMail))
|| (anyMails && model.MailChoices.Contains(c.Contact.CommunicationPreferences.TPEMail))
|| (anyPhones && model.PhoneChoices.Contains(c.Contact.CommunicationPreferences.TPTelephone)));
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/426643.html
