none
Multiple views, same class? RRS feed

  • Question

  • Hi all,

    How can I set up LINQ to SQL so that there are multiple views that LINQ treats as all being of the same type?  As in, I have views V_DATA_ON_FIRST_OF_MONTH and V_DATA_ON_EACH_DAY, and both have columns ENTITY_ID, ENTITY_NAME, FOO, BAR of the same types.  How can I have one class, MyDataClass, represent the data in both views?  Or do I need to wrap these in table-valued functions to get LINQ to play nicely with them?

    Thanks,

        Don
    Tuesday, February 16, 2010 10:03 PM

Answers

  • Hi all,

    How can I set up LINQ to SQL so that there are multiple views that LINQ treats as all being of the same type?  As in, I have views V_DATA_ON_FIRST_OF_MONTH and V_DATA_ON_EACH_DAY, and both have columns ENTITY_ID, ENTITY_NAME, FOO, BAR of the same types.  How can I have one class, MyDataClass, represent the data in both views?  Or do I need to wrap these in table-valued functions to get LINQ to play nicely with them?

    Thanks,

        Don

    It seems to me you could create an interface that is implemented by each of the views that contain the same columns.  It would look something like this:

    public interface IData
    {
        int ENTITY_ID
        {
            get;
            set;
        }
    
        string ENTITY_NAME
        {
            get;
            set;
        }
    
        <FOO type> FOO
        {
            get;
            set;
        }
    
        <BAR type> BAR
        {
            get;
            set;
        }
    }
    Then, assuming each of your views does indeed include the columns you defined in the interface with exactly the same names and types, all you would have to do is declare that the entitites implement the interface in the code-behind of your dbml file:
    public partial class V_DATA_ON_FIRST_OF_MONTH : IData
    {
    }
    
    public partial class V_DATA_ON_EACH_DAY : IData
    {
    }
    




    Tuesday, February 16, 2010 10:32 PM

All replies

  • Hi all,

    How can I set up LINQ to SQL so that there are multiple views that LINQ treats as all being of the same type?  As in, I have views V_DATA_ON_FIRST_OF_MONTH and V_DATA_ON_EACH_DAY, and both have columns ENTITY_ID, ENTITY_NAME, FOO, BAR of the same types.  How can I have one class, MyDataClass, represent the data in both views?  Or do I need to wrap these in table-valued functions to get LINQ to play nicely with them?

    Thanks,

        Don

    It seems to me you could create an interface that is implemented by each of the views that contain the same columns.  It would look something like this:

    public interface IData
    {
        int ENTITY_ID
        {
            get;
            set;
        }
    
        string ENTITY_NAME
        {
            get;
            set;
        }
    
        <FOO type> FOO
        {
            get;
            set;
        }
    
        <BAR type> BAR
        {
            get;
            set;
        }
    }
    Then, assuming each of your views does indeed include the columns you defined in the interface with exactly the same names and types, all you would have to do is declare that the entitites implement the interface in the code-behind of your dbml file:
    public partial class V_DATA_ON_FIRST_OF_MONTH : IData
    {
    }
    
    public partial class V_DATA_ON_EACH_DAY : IData
    {
    }
    




    Tuesday, February 16, 2010 10:32 PM
  • Cool.  Thanks for the alternative.
    Wednesday, February 17, 2010 2:12 PM