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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:51:24+00:00 2026-05-27T10:51:24+00:00

I have created an Update operation for Linq To Sql (L2S) which works like

  • 0

I have created an Update operation for Linq To Sql (L2S) which works like this:

var rowsaffected = db.Tables.Where(...).Join...Group..
                            .Update(x => new Table { Column = x.Something });

The problem is that L2S does not allow you to create entities within the query (the line above causes an exception at runtime).

To get around that I create a non-entity class like this:

class TableUpdate : Table { }

and use it in the Update

.Update(x => new TableUpdate { Column = x.Something });

which works fine.

The question is now if it is a good idea to create the TableUpdate class dynamically at runtime and just replace it in the expression tree before sending it to the L2S sql generator?

The class will never be instantiated so there is no code need, just metadata.

I assume performance is not problem (compared to the actual database update) but are there other thins to think about?

  • Can there be name collisions if I create the same class several times?
  • Can I cache the metadata?

Update

Solved it like this:

private static readonly Dictionary<Type, Type> _noEntityClasses = new Dictionary<Type, Type>(1);
private static ModuleBuilder _module;

private static Type CreateNoEntityClass(Type entityClass) {
    lock (_noEntityClasses) {
        Type noentityClass;
        if (!_noEntityClasses.TryGetValue(entityClass, out noentityClass)) {
            if (_module == null) {
                var assemblyName = new AssemblyName("LinqToSqlGenerated");
                var assemblyBuilder = Thread.GetDomain().DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Save);
                _module = assemblyBuilder.DefineDynamicModule("NonEntity");
            }
            var typeBuilder = _module.DefineType(entityClass.Name + "NoEntity", TypeAttributes.Class | TypeAttributes.NotPublic, entityClass);
            noentityClass = typeBuilder.CreateType();
            _noEntityClasses.Add(entityClass, noentityClass);
        }
        return noentityClass;
    }
}

private static IQueryable<T> RemoveEntity<T>(IQueryable<T> source, DataContext context) {
    var mapping = context.Mapping.MappingSource.GetModel(context.GetType());
    var noEntityExpression = source.Expression.Visit<MemberInitExpression>(expression => {
            if (!mapping.GetMetaType(expression.Type).IsEntity)
                return expression;
            return Expression.MemberInit(Expression.New(CreateNoEntityClass(expression.Type)), expression.Bindings);
                                                          });
    return source.Provider.CreateQuery<T>(noEntityExpression);
}
  • 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-27T10:51:25+00:00Added an answer on May 27, 2026 at 10:51 am

    Using RunSharp you can easily create a new class which extends from an existing one.

    You could create a class which creates these wrapped classes and keeps track for which types it already constructed the classes. Probably a Dictionary<Type, Type>, linking the original type to the dynamically created wrapped type.

    A static function in this class would simply initialize this wrapped type.

    public static object ExtendedClassInstance<T>()
    {
        Type type = typeof( T );
        if ( !_wrappedTypes.Contains( type ) )
        {
            // Use RunSharp to create the type.
        }
    
        return Activator.CreateInstance( _wrappedTypes[ type ] );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON array like this: { forum:[ { id:1, created:2010-03-19 , updated:2010-03-19
I have a JComboBox already created but I would like to update the information
I have created a PHP-script to update a web server that is live inside
I have created some simple charts (of type FastLine) with MSChart and update them
I have created a Data Access Layer using .NET. Everywhere that I update a
I have a question regarding an update function I created... CREATE OR REPLACE FUNCTION
I'm just getting started with Linq-to-SQL and data binding in WPF, most of which
What's the best practice for reading/saving/updating accented characters via HTML/MVC/LINQ-to-SQL? We have a web
I have a dbml file (linq to sql) to connect to my database. I
My selenium tests like to fails randomly. As an example I have this scenario

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.