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 8893845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:21:00+00:00 2026-06-14T23:21:00+00:00

What would be the best practice for setting a status depending on several other

  • 0

What would be the best practice for setting a status depending on several other “columns” retrieved in a linq query.

  var result = (from q in query
                select new Item
                         {
                           ApprovedDate = q.ApprovedDate,
                           CreatedDate = q.CreatedDate,
                           DeclinedDate = q.DeclinedDate,
                           Status = 0
                         });

I’d like to set the status to either 0, 1, 2.

(ApprovedDate == null and DeclinedDate == null) --> 0
(ApprovedDate != null and DeclinedDate == null) --> 1
(DeclinedDate != null) --> 3

So perhaps something like:

  var result = (from q in query
                select new Item
                         {
                           ApprovedDate = q.ApprovedDate,
                           CreatedDate = q.CreatedDate,
                           DeclinedDate = q.DeclinedDate,
                           Status = (q.CreatedDate == null && q.DeclinedDate == null) ? 0 : (q.ApprovedDate != null && q.DeclinedDate == null) ? 1 : 2
                         });

I might add even more status combinations, so should I try and do this in the linq select query, in my repository object.. Or later on in the controller where I would do a .ToList() and then foreach the list to set the correct status code?

Having even more than 3 statuscodes, the linq query gets “hard” to read.

  • 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-14T23:21:02+00:00Added an answer on June 14, 2026 at 11:21 pm

    What about moving status calculation to Item class? If status property depends on other properties value, then it’s definitely calculated property:

    var result = from q in query
                 select new Item
                             {
                               ApprovedDate = q.ApprovedDate,
                               CreatedDate = q.CreatedDate,
                               DeclinedDate = q.DeclinedDate
                             });
    

    And

    public class Item
    {
      // other properties
    
      public int Status
      {
          get
          {
              if (ApprovedDate == null and DeclinedDate == null)
                  return 0;
              if (ApprovedDate != null and DeclinedDate == null)
                  return 1;
              if (DeclinedDate != null)
                  return 3;
              // etc
          }
      }
    }
    

    Actually I think it’s best option, because in this case status calculation logic will be close to required data. If (for some reason) you can’t use this approach, then move setting statuses to local items collection:

    var items = result.ToList().ForEach(i => i.Status = CalculateStatus(i));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to know what's the best practice for setting up the distribution
What would be best practice to localize your ASP.NET MVC application? I would like
What would be best practice for allowing the user to store data for my
Hey all, what would be best practice for clipping the bottom borders of a
What would be the best practice to keep the user logged in if he
What would be the best practice for sharing localization object (in this case ResourceBundle,
Would it be considered best practice to use a single template that changes it's
What's the best practice of making a variable that would be accessible by almost
Which is the best practice in this situation? I would like an un-initialized array
What is a best practice when setting up subversion to use vendor branches? Our

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.