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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T14:00:31+00:00 2026-06-05T14:00:31+00:00

I have EF Code First solution which supports soft delete so we mark entities

  • 0

I have EF Code First solution which supports soft delete so we mark entities as IsDeleted and not remove them from database.
I would like to fiter collection of related objects (lazy loading) so entities marked as deleted are not returned to API user.

Here is simple approache I use:

public class FilteredCollection<T> : ICollection<T> where T : DeletableEntity
{
    private List<T> _listWithDeleted = new List<T>();

    protected IEnumerable<T> FilteredItems
    {
        get { return _listWithDeleted.Where(e => e.IsDeleted == false); }
    }

    protected bool _IsReadOnly;

    public virtual T this[int index]
    {
        get
        {                
            return FilteredItems.ToList()[index];
        }
        set
        {
            FilteredItems.ToList()[index] = value;
        }
    }

    public virtual int Count
    {
        get
        {
            return FilteredItems.Count();
        }
    }

    public virtual bool IsReadOnly
    {
        get
        {
            return _IsReadOnly;
        }
    }

    public virtual void Add(T entityObject)
    {
        _listWithDeleted.Add(entityObject);
    }

    public virtual bool Remove(T entityObject)
    {
        if (FilteredItems.Contains(entityObject))
        {
            entityObject.IsDeleted = true;
            return true;
        }
        else
        {
            return false;    
        }
    }

    public bool Contains(T entityObject)
    {
        return FilteredItems.Contains(entityObject);
    }

    public virtual void CopyTo(T[] entityObjectArray, int index)
    {
        var list = FilteredItems.ToList();
        list.CopyTo(entityObjectArray, index);
    }

    public virtual void Clear()
    {
        foreach (var item in _listWithDeleted)
        {
            item.IsDeleted = true;
        }
    }

    public virtual IEnumerator<T> GetEnumerator()
    {
        return FilteredItems.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return FilteredItems.GetEnumerator();
    }
}

I have implemented ICollection so it filter object internally. I use this class instead of ICollection so filtered entities are returned to me.
I have done some tests and it seems it works well but I don’t feel comfortable about this solution.

Could you please provide me with downsides of this approach or please suggest in case you know some better one.

Thanks beforehand,

-Petro

  • 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-05T14:00:33+00:00Added an answer on June 5, 2026 at 2:00 pm

    It is wrong solution because it makes filtering in your application. If your database will grow and there will be more and more soft deleted items you will always need to load all of them prior to applying filter. This can quickly become very big performance issue.

    You should do filtering in the database but that goes against lazy and eager loading. The solution can be conditional mapping (only not deleted items will be mapped) but it will not allow you mapping IsDeleted property – you will have to soft delete your entity with stored procedure and that in turn cannot be mapped with code first.

    With combination of EF code first + soft delete you should avoid lazy and eager loading completely and either use separate query to get filtered data or use explicit loading.

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

Sidebar

Related Questions

I have a big page with much spaghetti code. I'm not sure which solution
I have a code first model that I'm using in a project. But now
I have some code that first selects an option value in a dropdown menu
Using EF Code First I have an model object that has multiple properties that
i have probleme with entityframework code first , when i execute my app and
I have an Entity in Code First Entity framework that currently looks like this:
I have an issue in entity framework code first, three table in database as
I'm using Ef 4 code first and have a parent table with a child
I have an MVC 4 project using EF 5 Code First. I am trying
I have the following classes in an Entity Framework 4.3 Code-First Model for MVC3:

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.