Last post Oct 31, 2016 06:11 AM by Jean Sun
Oct 28, 2016 04:16 AM|prosenba|LINK
Having issues using LINQ with large list and than filtering list by other lists, if one of the list I'm using to filter with is empty the results comes back empty. I was hoping that if a list I'm using is filtered there would be away to make LINQ Ignore
it empty lists. I could work around creating a if statement and if a list is empty don't use it but want to be dynamic and less code.
List<string> allocationFilters = new List<string>();
List<string> lastNameFilters = new List<string>();
List<string> appealCodeFilters = new List<string>();
List<string> recurringFilters = new List<string>();
filterRecords = (from donorRecords in cachedRecords.RecordsList
from lastname in lastNameFilters
from appeal in appealCodeFilters
from allocations in allocationFilters
from recurring in recurringFilters
(lastNameFilters.Count() == 0 || donorRecords.EntityLastName.Contains(lastname))
(appealCodeFilters.Count() == 0 || donorRecords.AppealCode.Contains(appeal))
(recurringFilters.Count == 0 || donorRecords.RecurringFlag.ToString() == recurring)
(allocationFilters.Count == 0 || donorRecords.AllocationList.Where(m => m.AllocationCode == allocations).Any())
Oct 31, 2016 06:11 AM|Jean Sun|LINK
but want to be dynamic and less code.
I think your above code is necessary to meet your requirement, it is impossible to make it dynamic and less code.