2

I have some linq code that I am trying to refactor because its not very good:

Basically, I am wondering if there is a better way to perform the following:

if (!string.IsNullOrWhiteSpace(_filter.AssignedTo)
{
var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null 
            && cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo                                                   
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };
}
else
{
var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null 
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };
}

They are both identical apart from the where clause is checks for AssignTicketToUser.

I am hoping there is a nicer way to do this to avoid having to use an if else statement? I have a few of these code blocks and dont want to be duplicating code alot!

3 Answers 3

8
var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null 
            && (string.IsNullOrWhiteSpace(_filter.AssignedTo) ? true : cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo)                                                   
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };

You can get rid of the if-else statement altogether. Transfer the if condition it to the 2nd where clause, and remove the !. That second where clause becomes a ternary operator.

If the condition is true, that is if _filter.AssignedTo is null, then don't test _filter.AssignedTo by returning true. If it's not null or empty, then proceed to the clause that was there in your original else block.

Sign up to request clarification or add additional context in comments.

2 Comments

Instead of condition1 ? true : condition2 you could write (condition1 || condition2).
thanks, this is exactly what I am looking for.... and feel embarrassed I didnt think of this myself (combination of a new baby and late night working!). Thanks
3

one way could be:

var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null 
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };

if (!string.IsNullOrWhiteSpace(_filter.AssignedTo)
    query = query.Where(w => cUser.GetUserNameUsingGUID(w.AssignTicketToUser) == _filter.AssignedTo));

Comments

1

Take a look at the PredicateBuilder implementation from C# In a Nutshell, the And method should address your issue here, in a more generic way, and help build an understanding of LINQ and expression trees. You would end up with something like:

var query = from ticket in dataClassesDataContext.TicketsIssues
            where ticket.ClosedDate == null
            select new
            {
                Priority = ticket.TicketPriority.TicketPriorityName,
                Description = ticket.Description.Replace("\n", ", "),
            };

if (!string.IsNullOrWhiteSpace(_filter.AssignedTo))
{
     query = query.And(ticket => cUser.GetUserNameUsingGUID(ticket.AssignTicketToUser) == _filter.AssignedTo);
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.