none
Linq loading and setting properties RRS feed

  • Question

  • Hi all,

    I have the following class:

        [Table(Name="titles")]
        public class Title : IObject, IBase
        {
            #region Local variables
    
            private string description;
    
            #endregion
    
            #region Initialization
    
            protected override void Initialize()
            {
                var columns = CurrentUser.Database.Mapping.MappingSource
                                .GetModel(typeof(DataContext))
                                .GetMetaType(typeof(Title))
                                .DataMembers;
            }
    
            public Title()
            {
                Initialize();
            }
    
            public Title(int ID)
            {
                ErrorsEnum err = ErrorsEnum.errSuccess;
    
                Initialize();
    
                if (ID > 0)
                    err = Load(ID);
            }
    
            #endregion
    
            #region Properties
    
            //Public
            [Column(IsPrimaryKey = true, Name = Const.Columns.ID_NO, Storage = Const.Fields.ID, DbType = "int not null", IsDbGenerated = true)]
            public int ID
            {
                get { return this.id; }
                protected set { this.id = value; }
            }
            
            [Column(Name = "title", Storage = Const.Fields.DESCRIPTION)]
            public string Description
            {
                get { return this.description; }
                set { this.description = value; }
            }
    
            //Private
            [Column(Name = Const.Columns.ID_NO_PRIMARYGROUP, Storage = Const.Fields.PRIMARYGROUPID)]
            private int PrimarygroupID
            {
                get { return this.primarygroupID; }
                set { this.primarygroupID = value; }
            }
    
            #endregion
    
            #region Methods
    
            private ErrorsEnum Load(int ID)
            {
                ErrorsEnum err = ErrorsEnum.errSuccess;
    
                try
                {
                    var obj = (from t in CurrentUser.Database.Titles
                               where t.ID == ID
                               select t)
                              .First();
    
                    if (obj != null)
                    {
                        this.ID = obj.ID;
                        this.PrimarygroupID = obj.PrimarygroupID;
                        this.Description = obj.Description;
                    }
                    else
                        err = ErrorsEnum.errLoadFailed;
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e);
                    err = ErrorsEnum.errLoadFailed;
                }
    
                return err;
            }
    
            public override ErrorsEnum Save()
            {
                ErrorsEnum err = ErrorsEnum.errSuccess;
    
                try
                {
                    this.PrimarygroupID = CurrentUser.PrimarygroupID;
    
                    CurrentUser.Database.Titles.InsertOnSubmit(this);
                    CurrentUser.Database.SubmitChanges(ConflictMode.FailOnFirstConflict);
                }
                catch (Exception e)
                {
                    System.Diagnostics.Debug.WriteLine(e);
                    err = ErrorsEnum.errInsertFailed;
                }
    
                return err;
            }
    
            #endregion
        }

    This is a very basic class that has just 2 properties but other classes can have much more. So instead of having 50+ variables I was thinking of using a Dictionary object to store the column names and their values. Something that I'm trying to do in the Initialize() method.

    2 questions:

    1. How can I do this?

    2. The Load() method currently selects the record and copies it into the local variables. Is there a quicker & smarter way to do this?

    Thanks,

    Ivan

    • Moved by Leo Liu - MSFT Tuesday, March 13, 2012 6:41 AM Moved for better support. (From:Visual C# General)
    Monday, March 12, 2012 12:43 PM

Answers

  • Hi Cryo75,

    Welcome!

    >>So instead of having 50+ variables I was thinking of using a Dictionary object to store the column names and their values.

    I'm not very sure about your requirements, You can use MappingSource.GetModel to find all column in matadata. If there are many records in database, how to store so much records in dictionary. I think you may want to cache some ID(Piamary key), you can query them when the record needed.

    BTW, I don't very agree to put your logic in designer.cs file, you can put your logic in BLL.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 14, 2012 7:37 AM
    Moderator

All replies

  • Which line of your code is the error and what is the error code?

    chanmm


    chanmm

    Monday, March 12, 2012 4:02 PM
  •             //Get columns
                var columns = CurrentUser.Database.Mapping.MappingSource
                                .GetModel(typeof(DataContext))
                                .GetMetaType(typeof(Title))
                                .DataMembers;

    The above code gets the table's columns. So far so good. Now I would like to place those columns in a dictionary so that in the properties I can use the dictionary instead of having many private variables.

    Does this make sense?

    Ivan

    Tuesday, March 13, 2012 7:44 AM
  • Hi Cryo75,

    Welcome!

    >>So instead of having 50+ variables I was thinking of using a Dictionary object to store the column names and their values.

    I'm not very sure about your requirements, You can use MappingSource.GetModel to find all column in matadata. If there are many records in database, how to store so much records in dictionary. I think you may want to cache some ID(Piamary key), you can query them when the record needed.

    BTW, I don't very agree to put your logic in designer.cs file, you can put your logic in BLL.

    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 14, 2012 7:37 AM
    Moderator
  • Hi,

    I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?
    If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.
    Have a nice day.


    Alan Chen[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, March 19, 2012 8:09 AM
    Moderator