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

  • Home
  • SEARCH
  • 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 6095907
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T12:51:55+00:00 2026-05-23T12:51:55+00:00

I’m trying to load a collection of entities using Linq2SQL. The problem is, I

  • 0

I’m trying to load a collection of entities using Linq2SQL. The problem is, I dont know what the entities are,IList<object>. I have tried to select them using reflection, but I get an out of memory error when I do the select, I presume because the context is unable to parse my expression,and is loading everything from the DB.

If anyone has any advice on this, or an alternative way to do what I want, please let me know.

foreach (object entity in requiredEntities)
{
   Type entityType = entity.GetType();
   IQueryable<object> entityTable = (IQueryable<object>)dataContext.GetTable(entityType);               

   // grab the objects primary key field
   var pkeyField = entityType.GetProperties().SingleOrDefault(p =>
                   p.GetCustomAttributes(typeof(System.Data.Linq.Mapping.ColumnAttribute),true)
                   .Cast<System.Data.Linq.Mapping.ColumnAttribute>() 
                   .Any(attrib => attrib.IsPrimaryKey));

   object pkeyValue = pkeyField.GetValue(entity,null);

   Func<object,bool> primaryKeySelector = o => pkeyField.GetValue(o,null) == pkeyValue;

   // crash here, out of memory exception
   object result = entityTable.Where(primaryKeySelector).SingleOrDefault();
}
  • 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-23T12:51:56+00:00Added an answer on May 23, 2026 at 12:51 pm

    By using a delegate you are forcing it to use LINQ-to-Objects, which is why it is running out of memory. What you need to do is build an Expression instead. Equally, it is bad practice to use the attributes as that is not the only model that LINQ-to-SQL supports; it is preferable to look at dataContext.Mapping.GetMetaType(entityType) to get the primary key.

    If you have 4.0, the following should work:

    var entityType = typeof(User);
    var metaType = dataContext.Mapping.GetMetaType(entityType);
    var member = metaType.DataMembers.Single(m => m.IsPrimaryKey).Member;
    
    var param = Expression.Parameter(entityType);
    var body = Expression.Equal(Expression.MakeMemberAccess(param, member),
        Expression.MakeMemberAccess(Expression.Constant(entity), member));
    dynamic table = dataContext.GetTable(entityType);
    
    object result = Cheeky(table, body, param);
    

    with

    static T Cheeky<T>(ITable<T> source, Expression body, ParameterExpression param)
        where T : class
    {
        var predicate = Expression.Lambda<Func<T, bool>>(body, param);
        return source.SingleOrDefault(predicate);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to loop through a bunch of documents I have to put
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.