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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:05:22+00:00 2026-05-22T19:05:22+00:00

LINQ-2-SQL maintains an identity map so subsequent calls to entity.First(e => e.Id == id)

  • 0

LINQ-2-SQL maintains an identity map so subsequent calls to entity.First(e => e.Id == id) do not cause additional queries beyond the first one for a context.

Is there anyway to ask L2S if a particular item exists in the identity map?

I ask this cause there is support for .Attach which allows you to attach entities to a context, however the method will exception out if the item already exists in the identity map.

In an interoperability scenario I may want to load up entities in a different, faster orm and attach, however it makes no sense to lookup an entity if it is already in the identity map.

  • 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-22T19:05:23+00:00Added an answer on May 22, 2026 at 7:05 pm

    No nice way… you can hack your way in, though. For example imagine you have a User object that you know is keyed by Id:

    var user = // get some user
    var dataContext = // your DB context
    
    const BindingFlags AllInstance = BindingFlags.Instance | BindingFlags.NonPublic
            | BindingFlags.Public;
    object commonDataServices = typeof(DataContext)
        .GetField("services", AllInstance)
        .GetValue(dataContext);
    object identifier = commonDataServices.GetType()
        .GetProperty("IdentityManager", AllInstance)
        .GetValue(commonDataServices, null);
    MethodInfo find = identifier.GetType().GetMethod("Find", AllInstance);
    var metaType = dataContext.Mapping.GetMetaType(typeof(User));
    object[] keys = new object[] { user.Id };
    var user2 = (User)find.Invoke(identifier, new object[] { metaType, keys });
    bool pass = ReferenceEquals(user, user2);
    

    Pretty-much every access is non-public; I would expect this to suck performance-wise unless you use DynamicMethod to spoof access from another type.

    And as a DynamicMethod version:

    (yes, I’m hard-coding to an int key… you could make it any single-value by replacing the int with object, and just drop the OpCodes.Box, typeof(int) – or you could make it a params object[] argument and pass it in directly)

    static readonly Func<DataContext, Type, int, object> identityLookup = BuildIdentityLookup();
    
    static Func<DataContext, Type, int, object> BuildIdentityLookup()
    {
        var quickFind = new DynamicMethod("QuickFind", typeof(object), new Type[] { typeof(DataContext), typeof(Type), typeof(int) }, typeof(DataContext), true);
        var il = quickFind.GetILGenerator();
    
        const BindingFlags AllInstance = BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public;
        il.Emit(OpCodes.Ldarg_0); // DB
        var services = typeof(DataContext).GetField("services", AllInstance);
        il.Emit(OpCodes.Ldfld, services); // services
    
        var identifier = services.FieldType.GetProperty("IdentityManager", AllInstance);
        il.EmitCall(OpCodes.Callvirt, identifier.GetGetMethod(true), null); // identifier
    
        il.Emit(OpCodes.Ldarg_0); // identifier DB
        var mapping = typeof(DataContext).GetProperty("Mapping");
        il.EmitCall(OpCodes.Callvirt, mapping.GetGetMethod(), null); // identifier mapping
    
        il.Emit(OpCodes.Ldarg_1); // identifier mapping type
        il.EmitCall(OpCodes.Callvirt, mapping.PropertyType.GetMethod("GetMetaType"), null); // identifier metatype
    
        il.Emit(OpCodes.Ldc_I4_1); // identifier metatype 1
        il.Emit(OpCodes.Newarr, typeof(object)); // identifier metatype object[]
        il.Emit(OpCodes.Dup); // identifier metatype object[] object[]
        il.Emit(OpCodes.Ldc_I4_0); // identifier metatype object[] object[] 0
        il.Emit(OpCodes.Ldarg_2); // identifier metatype object[] object[] 0 id
        il.Emit(OpCodes.Box, typeof(int)); // identifier metatype object[] object[] 0 boxed-id
        il.Emit(OpCodes.Stelem_Ref); // identifier metatype object[]
    
        il.EmitCall(OpCodes.Callvirt, identifier.PropertyType.GetMethod("Find", AllInstance), null); // object
        il.Emit(OpCodes.Ret);
    
        return (Func<DataContext, Type, int, object>)quickFind.CreateDelegate(typeof(Func<DataContext, Type, int, object>));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In LINQ to SQL, is it possible to check to see if an entity
Can LINQ to SQL query using NOT IN ? e.g., SELECT au_lname, state FROM
With LINQ to SQL most likely going to not get as much active development
In LINQ to SQL, I get the exception The query operator 'ElementAt' is not
I use LINQ-SQL as my DAL, I then have a project called DB which
How to select all columns from tables in join using linq Sql: select CTRL_RUN_JOB.*,
LINQ to SQL allows table mappings to automatically convert back and forth to Enums
Linq to SQL creates objects which are IQueryable and full of relations. Html Helpers
Using LINQ to SQL, I have an Order class with a collection of OrderDetails.
In Linq to SQL it is possible to generate the database from the dbml

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.