Create your query one clause at the time instead of really bad-looking field == (filterSet ? filter : field)
.
First join all necessary tables:
var query = dataContext.Faults.Join(dataContext.Messages, f => f.FaultID, m => mFaultID, new { f, m })
.Join(dataContext.ContextProperties, x => x.m.MessageID, c => c.MessageID, new { x.f, x.m, c });
// add all other joins here
// and all where conditions that are always valid
Then add conditional where
conditions:
if(!string.IsNullOrEmpty(geniusReference))
{
query = query.Where(x => x.c.Value.ToString() == geniusReference);
}
// instead of:
// where (c.Value.ToString() == (!string.IsNullOrEmpty(geniusReference) ? geniusReference : c.Value.ToString())
At the end add projection:
var results = from x in query
select new FaultsWithFilters {
DateTime = x.f.DateTime
,FaultID = x.f.FaultID
,Scope = x.f.Scope
,FaultCode = x.f.FaultCode
,FaultSeverity = x.f.FaultSeverity
,ErrorType = x.f.ErrorType
,Description = x.f.Description
,FaultDescription = x.f.FaultDescription
,FailureCategory = x.f.FailureCategory
,GeniusReference = x.c.Value.ToString()
,ProgramNumber = x.cp.Value.ToString()
,Platform = x.cpp.Value.ToString()
,ModifiedBy = x.cppp.Value.ToString()
};