Is there a way to add or remove a line of an LINQ to DB Query based on if the value being checked is null?

If I have a query like this:

String Category = HttpContext.Current.Request.QueryString["Product"].ToString();

IQueryable<ItemFile> pressReleases = from file in connection.ItemFile
                                                           where file.Type_ID == 8
                                                                && file.Category == Category 
                                                           select file;

Is there a way to make this LINQ Query so that I do no use the line file.Category == Category if Category is null or empty?

Answers


How about don't add that where unless you need to?

IQueryable<ItemFile> pressReleases = from file in connection.ItemFile
                                     where file.Type_ID == 8
                                     select file;

if(!string.IsNullOrEmpty(Category)) {
    pressReleases = pressReleases.Where(file => file.Category == Category);
}

This works because LINQ queries are composable with deferred execution, so this filter is still part of the final TSQL.


This might not be exactly what you wanted but will render the same result:

String Category = HttpContext.Current.Request.QueryString["Product"].ToString();

IQueryable<ItemFile> pressReleases = from file in connection.ItemFile
                                     where file.Type_ID == 8
                                     && (string.IsNullOrEmpty(Category) || file.Category == Category)
                                     select file;

Need Your Help

iPhone Calendar Day View

iphone ios view calendar

Before jumping on me, please read my question carefully :D. I know there are already a few answers here, but there is none related about the actual view. I know how to implement the logic, using a

Readonly tkinter text widget

python text tkinter

I want to use tkinter text widget as a readonly widget. It should act as a transcript area. My idea is to keep this transcript in a file and whenever the user writes anything, just remove all the