Retrieving Reference Data in C# Code RRS feed

  • Question

  • I have a reference data tables in SQL with this SCHEMA

    StatusId (Primary Key)

    StatusType (Unique)


    StatusCode (Unique)

    In my code using LINQ, I query this table using the StatusCode column. I want to keep the StatusCode values in one place rather then hard coding it where ever I am querying for it.

    How to go about it?

    Thanks for your help in advance.

    Saturday, April 21, 2018 8:17 PM

All replies

  • Hello,

    The answer can go in several directions dependent on where you have data operations in e.g. in a form, in several forms, in a data classes for instance. Where are these coded needed going with the last statement of how you have written current data operations.

    If you are coding professionally you will have a data class which would have a method to obtain the codes once if they don't change for instance. Load the data once (again it depends on your current method of working with data) or as needed.

    Another thing to consider is how many rows are stored in the table, when retrieving data is t quick.

    Don't get hung up on LINQ, it's not the answer to every solution. Alternates include storing the data in a list of a concrete class which is much light weight that other options. Get the data via a connection, command and a reader off the command via a while loop.

    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    • Proposed as answer by Fei Hu Monday, April 23, 2018 8:35 AM
    Saturday, April 21, 2018 10:30 PM
  • Hi,

    StatusCodes (or any ReferenceData) is kind of constant data we use in our applications.
    The right approach for this is to create a separate Class Library project, create a static class in this project.
    In this class create constant string variables. e.g.

    public static class Constants
       public const string ENVIRONMENT = "Environment";

    You can refer this class library in your project/ any layer(DAL/BusinessLogic/UI).


    Sunday, April 22, 2018 9:40 PM