I have a method that accepts a date dtSince and a string component as parameters. I need to write an one-line linq query that searches for entries that occurred before dtSince and is of Component component (if specified). I have tried the following:
var result = from items in MyAzureTable
where items.Occured >= dtSince
&& items.Component == (component ?? items.Component)
select items;
But I’m getting a NotSupported error. I guess the items.Component == (component ?? items.Component) is the problem.
As mentioned, component can be null or empty. But I cannot exclude that in the original query, because this:
var result = from items in MyAzureTable
where items.Occured >= dtSince
select items;
might return more than 1000 rows (which appears to be the default limit of Azure tables) and therefore I cannot filter it with the component later. If I do something like the below, the entry I might be looking for is at row 1001. Thus, it won’t give me the result I am looking for.
if (!String.IsNullOrEmpty(component))
{
result = result.Where(x => x.Component == component).ToList<>();
}
Question: Is it possible to have a one line linq query that can check for a non-empty string first before using it in a where clause?
Try this: