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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:09:34+00:00 2026-05-18T05:09:34+00:00

I have a question about Entity Framework and Linq to Entities. My .NET version

  • 0

I have a question about Entity Framework and Linq to Entities. My .NET version is 4.0. I’m refactoring the database access layer of an existing application, and I plan to use Linq to Entities (instead of today’s DataReaders and SQL strings). The structure of the database cannot be altered.

My problem comes from a stored procedure, which, simplified, looks like the following:

CREATE PROCEDURE getElement @tableid as int, @elementid as int AS
BEGIN
DECLARE @tablename as varchar(50)
SELECT @tablename = tablename FROM tables WHERE tableid = @tableid
EXEC('SELECT * FROM ' + @tablename + ' WHERE elementid = ' + @elementid)
END

I know that the row(s) returned will have a column named elementid, and based on this value, I know what other columns to expect.

Today, this is solved with an SqlDataReader which does a dictionary-like lookup of the elemendid element.

public Element getElement(SqlDataReader dr)
{
    switch((int)dr["elementid"])
    {
        case 1:
            return getTextElement(dr);
        case 2:
            return getImageElement(dr);
        //...
    }
}

Element is an abstract base class. getTextElement returns a TextElement : Element, and getImageElement returns an ImageElement : Element.

How do I model this in Entity Framework? Complex types does not seem to cut it, since it does not seem to support dynamic properties. I have also looked at an EntityObject Generator, but I’m not really all that experienced with customizing T4 code (maybe I ought to learn for this problem?). The perfect solution for me would be to have the imported stored procedure return an object with the dynamic type, but Entity Framework 4 does not seem to support this.

  • 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-05-18T05:09:35+00:00Added an answer on May 18, 2026 at 5:09 am

    I just thought that I would add how I solved this.

    I created a couple of helper classes that emulates the behaviour of Linq to Entities, and use them on my special stored procedures. It’s far from perfect, or even good, but it makes the resulting code look quite similar to Linq to Entities. This is important for me as the rest of my database layer will use Linq to Entities.

    In the perfect world, I would be able to formulate a query to Linq to Entities, and then use the result somewhat similar to what I’m doing right now.

    Here we go…

    The code is used as follows:

    var connectionString = new SqlConnectionStringBuilder
    {
        DataSource = @"C:\Temp\Northwind.mdf"
    };
    
    var commandText = "select * from Customers";
    
    using (var rows = new SqlCommandHelper(connectionString.ToString(), System.Data.CommandType.Text, commandText))
    {
        foreach (dynamic row in rows)
        {
            try
            {
                Console.WriteLine(row.Fax ?? "Emtpy");
            }
            catch (IndexOutOfRangeException)
            {
                Console.WriteLine("Invalid column name");
            }
        }
    }
    

    As you can see, the enumeration of the rows looks similar to how it would be if I had used Linq to Entities instead of SqlCommandHelper.

    The SqlCommandHelper class is the following code:

    class SqlCommandHelper : IEnumerable<DynamicSqlRow>, IDisposable
    {
        private SqlConnection connection;
        private SqlCommand command;
    
        public SqlCommandHelper(string connectionString, System.Data.CommandType commandType, string commandText, params SqlParameter[] parameters)
        {
            connection = new SqlConnection(connectionString);
            command = new SqlCommand
            {
                CommandText = commandText,
                CommandType = commandType,
                Connection = connection
            };
    
            command.Parameters.AddRange(parameters);
        }
    
        public IEnumerator<DynamicSqlRow> GetEnumerator()
        {
            if (connection.State != System.Data.ConnectionState.Open)
            {
                connection.Open();
            }
    
            using (var reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    yield return new DynamicSqlRow(reader);
                }
            }            
        }
    
        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    
        public void Dispose()
        {
            command.Dispose();
            connection.Dispose();
        }
    }
    

    As you can see, the magic lies within DynamicSqlRow. I thing to note is that you’ll need to import the System.Dynamic namespace for DynamicSqlRow to compile.

    class DynamicSqlRow : DynamicObject
    {
        System.Data.IDataReader reader;
    
        public DynamicSqlRow(System.Data.IDataReader reader)
        {
            this.reader = reader;
        }
    
        public override bool TryGetMember(GetMemberBinder binder, out object result)
        {
            var row = reader[binder.Name];
    
            result = row is DBNull ? null : row;
    
            return true;
        }
    }
    

    I hope that this code might be useful for anyone else, or that it makes someone think of a better solution.

    A useful link for me was Walkthrough: Creating and Using Dynamic Objects from MSDN.

    Take care

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

Sidebar

Related Questions

I have a question about using streams in .NET to load files from disk.
I have a question about how to deploy WPF application into a PC without
iam new to Entity framework i have learned about using SP and how to
I have question about NSView: Imagine a Custom View where the mouseDown, mouseDrag and
I have a question about best practices regarding how one should approach storing complex
I have a question about locking. This doesn't have to be only about record
I have a question about using os.execvp in Python. I have the following bit
I have a question about using new[] . Imagine this: Object.SomeProperty = new[] {string1,
I have a question about this question . I posted a reply there but
I have a question about tables in MySQL. I'm currently making a website where

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.