Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8989377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T22:14:38+00:00 2026-06-15T22:14:38+00:00

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

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T22:14:40+00:00Added an answer on June 15, 2026 at 10:14 pm
    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some LINQ code that generates a list of strings, like this: var
I have some Linq code and it's working fine. It's a query that has
I'm trying to write some LINQ To SQL code that would generate SQL like
Can I have some help in improving this linq. I'm basically returning speakers from
I have a LINQ query that returns some object like this... var query =
I'm trying to rewrite some old SQL into LINQ to SQL. I have a
I have some code that queries Active Directory to verify user existence. I am
I'm trying to convert some old code that directly builds SQL queries to Entity
I'm currently trying to understand some of the fundamentals with LINQ. I have been
I am trying to get some C++ code working under C#. I have been

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.