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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T10:39:56+00:00 2026-06-18T10:39:56+00:00

Essentially I’m trying to find a less awful way to do this: foreach (var

  • 0

Essentially I’m trying to find a less awful way to do this:

foreach (var k in someList)
{
    try
    {
        var temp = Database.Set(k.GetType()).Local;
        newList.Add(k);
    }
    catch (InvalidOperationException)
    {

    }
}

Database is my DbContext instance for my model.

someList is a collection of objects, some are part of an Entity model, others are not. I want to create a new list (newList) that contains only the objects that are a part of the model. The objects in someList can be of any type (in my case one of these is List<string> which obviously has nothing to do with my underlying database).

The InvalidOperationException is raised when an object from someList is not a part of the Entity model. By doing this I get what I want, however it seems like a hack. I’m looking for a better solution.

  • 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-06-18T10:39:57+00:00Added an answer on June 18, 2026 at 10:39 am

    I’m adding this answer so that anyone who finds this question can see how I resolved it. That being said, the real solution is to avoid having entities and non-entities in the same collection (as said in the comments to the original question).

    To filter the collection someList you need to know which types are entities, and which are not. To do that I constructed a list of types from the properties of my DbContext.

    types = (from t in typeof (Entities).GetProperties()
             where t.PropertyType.IsGenericType
             where
                 t.PropertyType.GetGenericTypeDefinition() ==
                 typeof (DbSet<object>).GetGenericTypeDefinition()
             select t.PropertyType.GetGenericArguments()[0]).Distinct();
    

    Entities is the class which represents my database model (it inherits from DbContext).

    This works by locating all the DbSet<T> properties in Entities, and then constructing a collection of all the T types. Each of these types represents an entity type.

    To filter someList I simply check if the type of each member is contained in the collection types.

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

Sidebar

Related Questions

Essentially, I'm trying to find a DRYer way to represent this code: <li><g:link controller=ActivityDashboard
Essentially I want a border-less, black window which I can set the location and
Essentially, is there any way to make this code legal? main = print .
Essentially the title of this question explains the essense of what I am trying
Essentially what I'm trying to do at this point is build a program to
Essentially I'd like to achieve this: vars <- c(x1, x20, x37, etc) summary(data[vars]) However,
Essentially, what I'm trying to do is to load/draw a map from a Tiled
Essentially Im trying to get many many java clients connect to a socket on
Essentially, I'm looking for a way to transform GenericList<TInput> to GenericList<TOutput> , where GenericList
Essentially, I had this idea in my head for a sort of evolution simulator,

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.