我正在尝试使用 Linq 检索实体的所有记录,条件是参数中为真的属性之一在记录中为真。
我正在尝试实现逻辑“或”,请查看代码,因为它会更有意义。
在 FetchXML 中它正在工作:
public void GetLock(bool account = false, bool contact = false, bool incident = false)
{
var fetch = @"
<fetch mapping='logical'>
<entity name='de_processimport'>
<attribute name='de_processimportid' />
<filter type='or'>";
if (account)
fetch += @"<condition attribute='de_processingaccount' operator='eq' value='true' />";
if (contact)
fetch += @"<condition attribute='de_processingcontact' operator='eq' value='true' />";
if (incident)
fetch += @"<condition attribute='de_processingincident' operator='eq' value='true' />";
fetch += @"
</filter>
</entity>
</fetch>";
var processing = service.RetrieveMultiple(new FetchExpression(fetch));
...
}
在 Linq 中它不起作用:
public void GetLock(bool account = false, bool contact = false, bool incident = false)
{
var processing = linq.de_processimportSet
.Where(p => // get records that are processing the entities we are processing
(account) ? p.de_ProcessingAccount == true : false // get processImport records where processingAccount is true or otherwise ignore this clause (false)
|| (contact) ? p.de_ProcessingContact == true : false
|| (incident) ? p.de_ProcessingIncident == true : false
);
...
}