none
Default condition Linq + EF RRS feed

  • Question

  • Hi everybody.

    I need to know if there are an way to define a default filter in relations of mapping of the EF.

    I have an app that uses logical delete (flag 'Active' for each record), and every linq-select I need to put this 'where' condition.

    I'm using EF Code First, are there any way to automate this condition, in the relations? for example :

    var groups = person.Groups; // -> only active groups.

    Today, unfortunatelly, I have to do this, every single time:

    var groups = person.Groups.Where(g =. g.Active == true); // -> only active groups.

    • Moved by Leo Liu - MSFT Thursday, April 12, 2012 8:52 AM Moved for better support. (From:Visual C# General)
    Wednesday, April 11, 2012 1:24 PM

Answers

  • Hi Eduardo,

    Have you considered implementing an extension method, similar to the following:

    public IEnumerable<Person> Groups(this IEnumerable<Person> people)
    {
            return people.Where(g => g.Active);
    }



    • Edited by efwefwefwefwef Wednesday, April 11, 2012 1:31 PM Formatting
    • Marked as answer by Eduardo Spaki Friday, April 13, 2012 12:57 PM
    Wednesday, April 11, 2012 1:31 PM

All replies

  • Hi Eduardo,

    Have you considered implementing an extension method, similar to the following:

    public IEnumerable<Person> Groups(this IEnumerable<Person> people)
    {
            return people.Where(g => g.Active);
    }



    • Edited by efwefwefwefwef Wednesday, April 11, 2012 1:31 PM Formatting
    • Marked as answer by Eduardo Spaki Friday, April 13, 2012 12:57 PM
    Wednesday, April 11, 2012 1:31 PM
  • Hi,

    This is just Syntax suger in C#, I think where is good to understand what you're doing, am i right?


    I am fish.

    Thursday, April 12, 2012 9:28 AM