LINQ查询中筛选IEnumerable<string>非空记录报错,如何解决?
The error you're seeing happens because LINQ to Entities can't translate comparisons of IEnumerable<T> types (like your ModifiedAmounts property) to SQL. Additionally, the ModifiedAmounts sequence will never actually be null—it will be an empty collection if there are no matching Expenditure records for a scheme.
To fix this, you need to adjust your filter to check the contents of the sequence instead of comparing it to null. Here are two common solutions based on what you're trying to achieve:
1. Filter for records with at least one ModifiedAmount entry
If you want to keep only schemes that have at least one associated Expenditure (so ModifiedAmounts isn't empty), use the Any() method to check if the sequence contains elements:
if (rbList2.SelectedIndex == 6) { result = result.Where(a => a.ModifiedAmounts.Any()); }
2. Filter for records with at least one non-null ModifiedAmount
If you specifically want schemes where at least one ModifiedAmounts value in the group is not null, add a predicate to the Any() method:
if (rbList2.SelectedIndex == 6) { result = result.Where(a => a.ModifiedAmounts.Any(ma => ma != null)); }
Both of these approaches are translatable to SQL by LINQ to Entities, so they won't throw the error you encountered. Choose the one that matches your actual filtering requirement.
内容的提问来源于stack exchange,提问作者AKA

