LINQ To Entities Custom Expressions RRS feed

  • Question

  • Hi,

    I have this expression

    _context.Products.Where(r => r.DateDiscontinued != null)

    but I'd like to write it in this way

    _context.Products.Where(r => r.IsDiscontinued)


    But Product entity doesn't have the "direct" IsDiscontinued property,

    so is there any way of doing this?


    Many thanks

    Tuesday, March 15, 2011 12:34 PM


All replies

  • You can extend the class because is a partial class, you can add a property that evaluate the expresion you want and return a bool, this is pretty handy sometimes


    Tuesday, March 15, 2011 4:44 PM
  • Sure I did it already, that's not a problem, but linq doesn't support that "custom" property, it requires the native entity properties only (I'm talking about LINQ To Entities and only)


    Tuesday, March 15, 2011 5:22 PM
  • Well, this is the LINQ to SQL forum, I'm not an expert on Linq to Entities (not that I'm an expert on LInq to SQL either), try asking in the right forum but AFAIK is not possible


    Tuesday, March 15, 2011 5:28 PM
  • Hello,


    Thanks for posting here.

    According to your description, I think you're talking about the user-defined function in Entity Framework.

    One of the capabilities of EF since EF1 was the creation of custom functions inside the SSDL part of the model. After their creation, we could consume them like other imported functions. In that way, we could define functions in the model which were acting like stored procedures but without being a part of the database. This, of course, means that in order to use this feature, we needed to write some XML code inside the SSDL.

    The first thing to do is to open the model in XML editor and to add a Function element inside the Schema element in the SSDL part of the XML. Inside the Function element, we add a CommandText element which will hold our T-SQL expression. We can also add to the function parameters.

    More information please read this in the codeproject.


    I hope this can help you.


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, March 18, 2011 8:51 AM
  • You can do this but it does require a few extra helper classes added to your code to make the necessary interception into the expression tree, check out


    Friday, March 18, 2011 3:37 PM
  • Thanks Damien,

    that's what I needed :)



    Saturday, May 7, 2011 5:44 PM